24
votes

I need completely close my application when a user closes the document window using the applications at runtime.

Currently when they click the window, the application stays open and is displaying its menu bar. I read this article for iOS and to add an entry into the plist file: http://developer.appcelerator.com/question/40081/ios4---make-app-not-run-in-background

What is the easiest way for me to close the entire app when a user closes the document window?

3

3 Answers

54
votes

Is this a Mac app? If so, have your NSApp delegate implement applicationShouldTerminateAfterLastWindowClosed::

- (BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)application
{
    return YES;
}

Using Swift, it would be:

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
     return true
}
6
votes

When the button is pressed:

[NSApp terminate:self];
1
votes

Add the NSWindowDelegate in YourWindowController.h file and then add the following method in YourWindowController.m file:

-(void)windowWillClose:(NSNotification *)notification
{
    // You can use either of the following lines:
    [[NSApplication sharedApplication] terminate:nil];

    // OR
    exit(0);
}

You will need to use the NSWindowDelegate for all your windows.