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

后端 未结 5 1001
日久生厌
日久生厌 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:38

    This works for me.

    extension MainWindowController: NSWindowDelegate {
        func windowWillClose(_ notification: Notification) {
            if let window = notification.object as? NSWindow, let controller = window.windowController {
    
                if window == self.window {
    
                    for window in self.childWindows {
    
                        print(" Closing \(window)")
                        window.close()
    
                    }
    
                }
            }
        }
    }
    

提交回复
热议问题