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

后端 未结 5 1002
日久生厌
日久生厌 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条回答
  •  盖世英雄少女心
    2021-02-05 05:46

    As the question is mainly about Cocoa programming and not about a specific language (Objective-C), here is the Swift version of Chuck's and Steve's answer:

    @NSApplicationMain
    class AppDelegate: NSObject, NSApplicationDelegate {
    
        func applicationShouldTerminateAfterLastWindowClosed(sender: NSApplication) -> Bool {
            return true
        }
    
        // Your other application delegate methods ...
    
    }
    

    For Swift 3 change the method definition to

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

提交回复
热议问题