7

我正在尝试编写此警报:

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

请告诉我我做错了什么。

谢谢

4

1 回答 1

4

您应该从中运行代码,viewDidAppear因为您的视图控制器尚未在viewDidLoad.

override func viewDidAppear() {
    super.viewDidAppear()

    alertUser()
}
于 2016-09-17T17:49:49.523 回答