PDA

View Full Version : Closing The About Box Terminates The App?




Blakeasd
Apr 30, 2011, 12:09 AM
Hello,
I am writing a Cocoa application and I am running into a problem, when closing the about box to my app the application terminates. I call the following method so that when my main window closes the app terminates:


-(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender{

return YES;

}


The main window to my app is an HUD panel so I think this may be causing the problem
Any Solutions? I would leave it alone, but my app is rejected because Apple considers it to be a bug.



kainjow
Apr 30, 2011, 12:17 AM
I'm guessing what's happening is since your window is an NSPanel it is not registered as a main window, so after the About box closes, it doesn't have a main window anymore and terminates. You could try subclassing NSPanel and overriding canBecomeMainWindow and return YES, then use this subclass in your nib.

jiminaus
Apr 30, 2011, 12:24 AM
Because your "main" window is a NSPanel, it's responding NO to canBecomeMainWindow. In Cocoa terms, you don't have a main window. That's why applicationShouldTerminateAfterLastWindowClosed: is being sent to your application delegate when the user closes the about box. You're unconditionally responding with YES, so you're app terminates.

You have 2 choice.

a) Subclass NSPanel so canBecomeMainWindow responds YES. That way you will have a main window and your application delegate won't be sent applicationShouldTerminateAfterLastWindowClosed: when the about box is closed.

b) Or check to see if you're HUD panel is open in applicationShouldTerminateAfterLastWindowClosed: and respond NO if it is.

EDIT: What kainjow said. :)

Blakeasd
Apr 30, 2011, 08:39 AM
I subclassed NSPanel and did the following:



-(BOOL)canBecomeMainWindow{

return YES;

}



This doesn't seem to work though :(

kainjow
Apr 30, 2011, 02:40 PM
You're right.

I looked at the docs for applicationShouldTerminateAfterLastWindowClosed and it says:
It sends this message regardless of whether there are still panels open. (A panel in this case is defined as being an instance of NSPanel or one of its subclasses.)

So you need to handle this somehow, possibly like this:
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
{
for (NSWindow *win in [NSApp windows]) {
if ([win isVisible]) {
return NO;
}
}
return YES;
}

Blakeasd
Apr 30, 2011, 03:25 PM
Thanks @kainjow the about box doesn't terminate the app anymore