windowShouldClose(_:) 从未调用过

Phi*_*oLu 2 osx-elcapitan swift3 xcode8

我想阻止用户在倒计时运行时关闭主窗口(并退出应用程序)。我已经阅读了有关该问题的帖子,但不幸的是,所解释的方法均无效。我认为我做了正确的事情,但 windowShouldClose 从未像其他两个函数那样被调用。生无可恋。:) 这是我在 NSWindowController 中的代码:

import Cocoa

class WindowController: NSWindowController, NSWindowDelegate {

override func windowDidLoad() {
    super.windowDidLoad()
    print("Window did load")
    self.window?.delegate = self
}

func windowShouldClose(sender: NSWindow) -> Bool {
    print("Window should close")
    let alert = NSAlert.init()
    alert.addButton(withTitle: "No")
    alert.addButton(withTitle: "Yes")
    alert.informativeText = "Close the window?"
    let response = alert.runModal()
    if response == NSAlertFirstButtonReturn {
        return true
    } else {
        return false
    }
}

func windowWillClose(_ notification: Notification) {
    print("Window will close")
}

func windowDidChangeScreen(_ notification: Notification) {
    print("Window did change screen")
}

}
Run Code Online (Sandbox Code Playgroud)

vad*_*ian 5

windowShouldClose(_:)从未被调用,因为您将其声明为windowShouldClose(:)

在 Swift 3 中,委托方法的签名是

func windowShouldClose(_ sender: NSWindow) -> Bool
Run Code Online (Sandbox Code Playgroud)