gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 23:09:25 25 4
gpt4 key购买 nike

我正在尝试编写此警报:

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 运行您的代码,因为您的 View Controller 尚未在 viewDidLoad 中创建窗口对象。

override func viewDidAppear() {
super.viewDidAppear()

alertUser()
}

关于swift - 我如何在最新的 swift 中编写 NSAlert?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39427088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com