Closing The About Box Terminates The App?

Discussion in 'Mac Programming' started by Blakeasd, Apr 29, 2011.

  1. Blakeasd
    Expand Collapse
    macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    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:
    Code:
    
    -(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.
     
  2. kainjow
    Expand Collapse
    Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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.
     
  3. jiminaus
    Expand Collapse
    macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    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. :)
     
  4. Blakeasd
    Expand Collapse
    thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #4
    I subclassed NSPanel and did the following:
    Code:
    
    
    -(BOOL)canBecomeMainWindow{
    
        return YES;
    
    }
    
    
    
    This doesn't seem to work though :(
     
  5. kainjow
    Expand Collapse
    Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    You're right.

    I looked at the docs for applicationShouldTerminateAfterLastWindowClosed and it says:
    So you need to handle this somehow, possibly like this:
    Code:
    - ([color=#aa0d91]BOOL[/color])applicationShouldTerminateAfterLastWindowClosed:([color=#5c2699]NSApplication[/color] *)theApplication
    {
        [color=#aa0d91]for[/color] ([color=#5c2699]NSWindow[/color] *win [color=#aa0d91]in[/color] [[color=#5c2699]NSApp[/color] [color=#2e0d6e]windows[/color]]) {
            [color=#aa0d91]if[/color] ([win [color=#2e0d6e]isVisible[/color]]) {
                [color=#aa0d91]return[/color] [color=#aa0d91]NO[/color];
            }
        }
        [color=#aa0d91]return[/color] [color=#aa0d91]YES[/color];
    }
     
  6. Blakeasd
    Expand Collapse
    thread starter macrumors 6502a

    Joined:
    Dec 29, 2009
    #6
    Thanks @kainjow the about box doesn't terminate the app anymore
     

Share This Page