我正在尝试写这个警报: func alertUser() { let alert = NSAlert() alert.messageText = "message 1" alert.informativeText = "info1" alert.informativeText = "info2" alert.addButton(withTitle: "NO") alert.addButton(withTitle: "YES") al
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上打开一个Optional值消息时,我发现了可怕的意外发现的nil
请告诉我我做错了什么.
谢谢
您应该从viewDidAppear运行代码,因为您的视图控制器尚未在viewDidLoad中创建窗口对象.override func viewDidAppear() { super.viewDidAppear() alertUser() }