Trying to know when a window closes in a macOS Document based application

前端 未结 1 1687
故里飘歌
故里飘歌 2020-12-02 02:45

I\'m trying to know when a window closes, I implemented this code:

class ViewController: NSViewController, NSWindowDelegate {

    override func viewDidLoad(         


        
相关标签:
1条回答
  • 2020-12-02 03:19

    The problem there is that the window property will always return nil inside viewDidLoadMethod. You need to set the delegate inside viewWillAppear method:

    class ViewController: NSViewController, NSWindowDelegate {
        override func viewWillAppear() {
            super.viewWillAppear()
            view.window?.delegate = self
        }
        func windowWillClose(_ aNotification: Notification) {
            print("windowWillClose")
        }
    }
    
    0 讨论(0)
提交回复
热议问题