Cocoa: Close the app when a HUD panel is closed - objective-c

I need to close the entire app when the user click on a Panel close button,
I tried:
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
return YES;
}
but it closes the app also from file-selector panels.
Thank you in advance.
L.

Applications can't be closed, only quit. Only windows can be closed, not applications.
If you want to quit the application when only a specific window is closed, be that window's delegate and respond to the windowWillClose: message by telling the application to terminate.

Related

How to show window after pressing X

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.

App stuck on runModalForWindow

I'm trying to display a modal dialog on top of my app but it's blocking my main app window when it closes. Here's my code:
TutorialWindowController* pTutorialController = [[TutorialWindowController alloc] initWithWindowNibName:#"TutorialWindow"];
NSWindow* pTutorialWindow = [pTutorialController window];
DDLogInfo(#"Tutorial window opening...");
[NSApp runModalForWindow: pTutorialWindow];
DDLogInfo(#"Tutorial window closed!"); // CODE NEVER GETS HERE
[NSApp endSheet: pTutorialWindow];
[pTutorialWindow orderOut: self];
In the modal dialog, my Close button runs this:
- (IBAction)closeButtonPressed:(id)sender {
[NSApp stopModal];
}
The modal dialog displays fine. However, when I click the Close button, the dialog disappears and my app's main window isn't responsive. I hear the bonk every time I try clicking. I'm pretty sure this is because the code never continues after runModalForWindow. Same thing happens if I close the modal dialo using the red X.
What am I doing wrong?
After ordering out the tutorial window, try doing a
[window makeKeyAndOrderFront:self];
on your main window.
You should call [pTutorialWindow orderOut:nil] first.
Not sure about the closeButtonPressed handler. But try adding to the delegate:
- (void) windowWillClose:(NSNotification *)notification
{
// ...
// In there, you should verify that you are calling:
[NSApp stopModal]
}
Adding the stopModal call solved the issue for me.
Verify that the Window delegate in the Interface Editor's Connection Inspector is connected to the File's Owner.
I had several modal dialogues working correctly except for one, and the missing connection was the only difference. Making the connection fixed the problem.

NSWindow Modal Problems

I'm running a modal window in my application, which works fine. The problem however seems to be dismissing the window.
While the window closes, it takes at least 20 seconds for the application to continue (I show the modal window at startup), but it will continue instantly if I click on its dock icon.
Here's how I close the modal window:
[NSApp stopModal];
[updaterWindow orderOut:self];
[updaterWindow close];
I've tried various things to get the Application to continue faster, such as manually switching to the main run loop and speaking to the main window directly (asking it to become the key window), which all did not work. Same as before, 20 seconds wait or instant load when clicking on the dock icon.
What can cause something like this? I'm really baffled.
Edit:
My modal window is actually created in a really simple way. I run my window as modal:
[NSApp runModalForWindow:updaterWindow];
When I am done with things, I close it:
[NSApp stopModal];
[updaterWindow orderOut:self];
[updaterWindow close];
The window goes away, but it needs a click to the dock icon or anywhere on the screen for the app to continue.
I'm doing something similar in my app at the moment: I have an IBAction method that calls the modal window:
-(IBAction)showMyModalWindow:(id)sender {
[theModalWindow makeKeyWindow];
NSInteger retVal = [NSApp runModalForWindow:theModalWindow];
[theModalWindow close];
//continue and do somethin according the value in retVal
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}
In this case theModalWindow is in it's own nib file has an NSObject in it that deals with the ok and cancel button to dismiss theWindow, by way of example here's the cancel button callback:
-(IBAction)cancelButton:(id)sender
{
[NSApp stopModalWithCode:errAuthorizationCanceled];
}
The last line in showMyModalWindow: method seems to be what's needed in my case by sending the app forward... Hope that's of some use!
Todd.

Closing Mac application (clicking red cross on top) and reopening by clicking dock icon

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

Cocoa switching a focus to my application

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.