我试图写下此提醒:
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)
}
但是我在第unexpectedly found nil while unwrapping an Optional value
行
alert.beginSheetModal
消息
请告诉我我做错了什么。
由于
答案 0 :(得分:4)
您应该从viewDidAppear
运行代码,因为您的视图控制器尚未在viewDidLoad
中创建窗口对象。
override func viewDidAppear() {
super.viewDidAppear()
alertUser()
}