Make a Cocoa application quit when the main window is closed?

后端 未结 5 1003
日久生厌
日久生厌 2021-02-05 05:18

How to make a Cocoa application quit when the main window is closed? Without that you have to click on the app icon and click quit in the menu.

5条回答
  •  梦毁少年i
    2021-02-05 05:34

    You should have an IBOutlet to your main window. For Example: IBOutlet NSWindow *mainWindow;

    - (void)awakeFromWindow {
        [mainWindow setDelegate: self];
    }
    - (void)windowWillClose:(NSNotification *)notification {
        [NSApp terminate:self];
    }
    

    If this does not work you should add an observer to your NSNotificationCenter for the Notification NSWindowWillCloseNotification. Don't forget to check if the right window is closing.

提交回复
热议问题