swift – 试图了解基于macOS Document的应用程序窗口何时关闭

前端之家收集整理的这篇文章主要介绍了swift – 试图了解基于macOS Document的应用程序窗口何时关闭前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图知道窗口何时关闭,我实现了这段代码
class ViewController: NSViewController,NSWindowDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let window: NSWindow? = view.window
        window?.delegate = self
    }

    func windowWillClose(_ aNotification: Notification) {
        print("windowWillClose")
    }

}

不幸的是没有任何事情发生,我能犯错什么?

文件https://developer.apple.com/documentation/appkit/nswindow/1419400-willclosenotification

PS
我已经在没有找到解决方案的情况下阅读了这个问题:Handle close event of the window in Swift

问题是window属性将始终在viewDidLoadMethod中返回nil.您需要在viewWillAppear方法中设置委托:
class ViewController: NSViewController,NSWindowDelegate {
    override func viewWillAppear() {
        super.viewWillAppear()
        view.window?.delegate = self
    }
    func windowWillClose(_ aNotification: Notification) {
        print("windowWillClose")
    }
}
原文链接:https://www.f2er.com/swift/319538.html

猜你在找的Swift相关文章