I made an Mac OS X cocoa app, and when I click the red button the main window disappears.
However, when I clicked the icon in dock, it doesn't show the main window anymore.
What's wrong? How do i redraw the main window by catching what message?
You might be able to do something like this in your application delegate:
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender {
//show your window here
return NO;
}
Related
I am on objectiveC, OSX, not iOS. XCode 8.3
I have a preferences Window (custom NSWindow) that opens as a modal on my main window.
The preferences window itself contains a view with tabs. The tab height changes the windows size whenever one is clicked.
First Tab clicked:
Second Tab clicked:
Now if someone hides the application in the dock and activates it again, the preferences window becomes active with the height of tab 1, even if tab 2 is still active. So the content gets cut off.
What i need is some kind of notification that gets triggered on becoming active/visible again to trigger a resize of the window before it gets displayed.
I tried it with these notifications in my NSWindow subclass (with NSWindow delegate set).
- (void)windowDidResignMain:(NSNotification*)notification{
NSLog(#"windowDidResignMain");
}
- (void)windowDidResignKey:(NSNotification*)notification{
NSLog(#"windowDidResignKey");
}
- (BOOL)canBecomeKeyWindow{
return YES;
}
- (BOOL)canBecomeMainWindow{
return YES;
}
But none of them worked. Is it because it's a modal window?
Any help appreciated.
I found it. My mistake - my tabViewController triggered a resize on viewWillAppear with always the first tab. I changed that to the current selected tab and that was it.
I'm writing a Mac OS X application using Xcode 6 and Objective C.
After starting the application and press the X, the window will be hidden. Is there a way to show up the window with clicking on the App-Symbol on the dock (like in Safari)?
On clicking "X" or close dot then window closes and if it's a root Window then your app will be closed. So if you want to hide on clicking "X" or close dot then you could use below:-
//Called when clicked close option on window
- (BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSApplication *) theApplication
{
[[NSApplication sharedApplication] hide:self];
return NO;
}
//Called when you tap app icon on dock.
-(BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag{
[[self.mainWindowController window] makeKeyAndOrderFront:self];
return YES;
}
But remember when we hide a OS X app then after a certain time the OS will sleep that app ,called NSAppNap will take place and your background work will be delayed or simply OS will perform those operation in a batching form.
When the user clicks the "X" (or red "Close" dot), that actually fully closes the window and does not hide it.
To make the window reappear (assuming it's still in memory and not released thanks to ARC), you need to do "showWindow" on the window controller. And when clicking on the dock icon, the best place to catch the dock icon being clicked might be NSApplicationDelegate's "applicationDidBecomeActive:" notification.
There are many tabs in my screen,I want to give an alert box which says "Do you want to save the changes?" if user changes anything in the page, without clicking on the save button provided in the page,he is clicking on diff tab.
I'm able to get the alert view but the tab click moves the screen to the tab which was clicked. The screen should not change until the alert view is answered.
Can anyone let me know how to suppress the screen change until the alert view is answered ?
This doesn't directly answer your question, but: what you're trying to do sounds like bad UI design. (In general, if it feels like you are fighting against UIKit, you're probably doing it the wrong.)
In this case: if you really want to ensure that a user taps a Save button before moving to a different screen, you should present that screen in a modal view, so that it is impossible to navigate to any other part of the app.
In other words, if you want to prevent a user from navigating away from a screen, don't show them buttons or tabs that would allow them to navigate away. Otherwise, you're just making more work for yourself and frustrating a user.
Implement UITabBarControllerDelegate in your app delegate's applicationDidFinishLaunching
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
self.tabBarController.delegate = self;
[window addSubview:self.tabBarController.view];
}
Then use the below delegate method,
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController;
This method is called before the tab switch and you can return no here to disable that and show an alert message instead. Once the user has performed the save either he can press on tab again or you can programmatically switch to the new tab as,
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
Add this inside your delegate,
How about this for switching to the tab programmatically,
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if () {
//some code
} else {
//some other code
self.tabBarController.selectedViewController = viewController;
}
}
When I close my Mac application (by clicking red cross button on window top bar) the app icon stays in the dock at the bottom. Now this is normal behaviour. When user click on it again it does not fire up the application unless the user quits the application altogether and relaunches it again.
A similar example on Mac OS X is "Activity Monitor". You can close the application by clicking the red cross button at the top the but dock icon stays there. User can re-open it by clicking dock icon.
How can I achieve this in my own application ?
If you are still concerned how to reopen the window that you have closed, use this method:
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag {
[window makeKeyAndOrderFront:self];
return YES;
}
You can use this to handle clicks on the applications icon in the dock.
For further information check out the NSApplicationDelegate Protocol Reference.
Here is the documentation:
http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSApplicationDelegate_Protocol/Reference/Reference.html
Hope this helps!
Latest Update:
In latest Xcode 11.4 on MacOS 10.15 with Swift 5.2, this same problem exists in MacOS SwiftUI app. Adding following code inside AppDelegates.swift solves the issue.
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if !flag{
window.makeKeyAndOrderFront(nil)
}
return true
}
Implement the method
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
{
return NO;
}
in your app delegate
Your app will hang around after the window is closed and then if you implement
- (void)applicationDidBecomeActive:(NSNotification *)aNotification
{
//dock icon has just been clicked , or cmd-tabbed into
}
in the app delegate
You can do things when the icon is clicked such as open a new or old window if you need to
See http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSApplicationDelegate_Protocol/Reference/Reference.html for other relevant application events
I think that the answers above aren't fully correct, to achieve this you should override applicationShouldHandleReopen(_:hasVisibleWindows:) https://developer.apple.com/reference/appkit/nsapplicationdelegate/1428638-applicationshouldhandlereopen
I have a statusbar application (the one that runs in the top bar of the system). When i click on the icon in the statusbar, a menu pops up and it has an item to configure my app. When i press this item, the main app window is shown [wndMain makeKeyAndOrderFront:self]; The problem is clicking a statusbar icon doesn't activate the application and, if other application is active at that moment, the main window will open below that active application and will be out of focus. How can i programatically switch focus to my application when clicking on the statusbar icon so that the settings window would always open on top of other windows and would be focused and ready for user input?
Thank you
Send the shared NSApplication object an activateIgnoringOtherApps: message.
NSApplication *myApp = [NSApplication sharedApplication];
[myApp activateIgnoringOtherApps:YES];
[self.window orderFrontRegardless];
you can use the code above to solve the problem.