如何在最新 swift 中编写 NSAlert?
How do I write an NSAlert in latest swift?
我正在尝试编写此警报:
func alertUser() {
let alert = NSAlert()
alert.messageText = "message 1"
alert.informativeText = "info1"
alert.informativeText = "info2"
alert.addButton(withTitle: "NO")
alert.addButton(withTitle: "YES")
alert.beginSheetModal(for: self.view.window!) { (returnCode: NSModalResponse) -> Void in
print ("returnCode: ", returnCode)
}
但我在 alert.beginSheetModal
行收到可怕的 unexpectedly found nil while unwrapping an Optional value
消息
请告诉我哪里做错了。
谢谢
您应该 运行 来自 viewDidAppear
的代码,因为您的视图控制器尚未在 viewDidLoad
中创建 window 对象。
override func viewDidAppear() {
super.viewDidAppear()
alertUser()
}
我正在尝试编写此警报:
func alertUser() {
let alert = NSAlert()
alert.messageText = "message 1"
alert.informativeText = "info1"
alert.informativeText = "info2"
alert.addButton(withTitle: "NO")
alert.addButton(withTitle: "YES")
alert.beginSheetModal(for: self.view.window!) { (returnCode: NSModalResponse) -> Void in
print ("returnCode: ", returnCode)
}
但我在 alert.beginSheetModal
unexpectedly found nil while unwrapping an Optional value
消息
请告诉我哪里做错了。
谢谢
您应该 运行 来自 viewDidAppear
的代码,因为您的视图控制器尚未在 viewDidLoad
中创建 window 对象。
override func viewDidAppear() {
super.viewDidAppear()
alertUser()
}