当前位置 : 主页 > 手机开发 > 其它 >

如何在最新的swift中编写NSAlert?

来源:互联网 收集:自由互联 发布时间:2021-06-11
我正在尝试写这个警报: 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()
}
网友评论