Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Completely close an OS X application with window close button

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?

like image 970
DevCompany Avatar asked Nov 22 '11 20:11

DevCompany


3 Answers

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
}
like image 96
mipadi Avatar answered Nov 19 '22 09:11

mipadi


When the button is pressed:

[NSApp terminate:self];
like image 26
Liftoff Avatar answered Nov 19 '22 08:11

Liftoff


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.

like image 22
Erfan Avatar answered Nov 19 '22 09:11

Erfan