View Full Version : Cocoa Windows close button quites App?

Jan 15, 2005, 01:34 PM

This is probably so easy, as easy as checking a box in IB even. Anyway, I would like a way to make my Cocoa applications close button (the red button in the top left) quit the application like with System Preferences :cool:.

Thanks all,


Jan 15, 2005, 02:37 PM
So what you need to do first is have the window you want to close be connected to an IBOutlet in the nib. For this example i connected the window to an outlet named "mainWindow".

The code to add is:
- (void)awakeFromNib {
[mainWindow setDelegate:self];

- (void)windowWillClose:(NSNotification *)aNotification {
[NSApp terminate:self];
what this code does is overwrite the window closing action and tells it to quit instead. you do not need to add the "windowWillClose:" method to your header file.

Jan 16, 2005, 04:00 AM
Or use a delegate to the NSApplication object that returns YES to - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication

You can set any object to be the NSApplication object by putting something like [NSApp setDelegate:self]; in awakeFromNib.

Documentation here (http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/ObjC_classic/Classes/NSApplication.html#//apple_ref/doc/uid/20000012/applicationShouldTerminateAfterLastWindowClosed_)

Jan 16, 2005, 02:30 PM
Thanks a lot :) got it working fine with both methods, though I did the delegation in Interface Builder rather than programatically (just seemed like the right thing to do ;)).

Anyway, Is there a way to set NSApp's delegate from Interface Builder too or not?

Thanks again guys,


Jan 16, 2005, 02:34 PM
As you are not in a document based app the File's Owner is the NSApplication object. You can use this to set the delegate.

Edit: I think this is always true for the MainMenu.nib file. Anyone?

Jan 17, 2005, 08:18 AM
Ta Robbie :).

Feb 3, 2010, 10:55 AM
Thanks a lot for this topic, it works! :)

Feb 3, 2010, 12:00 PM
Is there a way to set NSApp's delegate from Interface Builder too or not?