How to quit cocoa app when windows close?

Objective CCocoa

Objective C Problem Overview


I need to quit Cocoa App when I click the red button on upper left.

enter image description here

I found http://forums.macrumors.com/archive/index.php/t-105229.html">this</a> page saying

> 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".

How can I do this? I found Windows in xib file, but how can I connect it to an IBOutlet in the nib?

Or, is there any way to quit the cocoa app clicking red button?

EDIT

I should have put the code in the automatically generated delegate file.

Objective C Solutions


Solution 1 - Objective C

There is an optional method for the application's delegate which will do this automatically. All you have to do is add this to the implementation. You don't need to create an outlet or anything.

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

Solution 2 - Objective C

Take a look at the NSApplicationDelegate protocol, especially to the applicationShouldTerminateAfterLastWindowClosed:method...

http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSApplicationDelegate_Protocol/Reference/Reference.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprosseekView Question on Stackoverflow
Solution 1 - Objective CughoavgfhwView Answer on Stackoverflow
Solution 2 - Objective CMacmadeView Answer on Stackoverflow