我想阻止用户在倒计时运行时关闭主窗口(并退出应用程序)。我已经阅读了有关该问题的帖子,但不幸的是,所解释的方法均无效。我认为我做了正确的事情,但 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 …Run Code Online (Sandbox Code Playgroud)