gpt4 book ai didi

ios - 为什么这个 UIAlertController 不显示?

转载 作者:可可西里 更新时间:2023-11-01 00:58:37 29 4
gpt4 key购买 nike

在尝试实现 UI 警报时,我遇到了一些问题。我在 Xcode 8 beta 4 中使用 swift 3.0,我试图让一个按钮激活警报,一个按钮(取消)解除警报,另一个按钮(确定)执行一个像 UIAction 按钮一样的操作,但是我一直无法甚至可以显示警报。

var warning = UIAlertController(title: "warning", message: "This will erase all content", preferredStyle: .Alert)

var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
}

var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}

warning.addAction(okAction) {
// this is where the actions to erase the content in the strings
}
warning.addAction(cancelAction)

self.presentViewController(warning, animated: true, completion: nil)

最佳答案

该代码与 Swift 3 不兼容。像 .Alert 这样的东西现在是 .alert。而 presentViewController 方法则完全不同。

这应该有效。

let warning = UIAlertController(title: "warning", message: "This will erase all content", preferredStyle: .alert)

let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in
NSLog("OK Pressed")
//ok action should go here
}


let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}

warning.addAction(okAction)
warning.addAction(cancelAction)

present(warning, animated: true, completion: nil)

为什么在 addAction(okAction) 之后而不是在创建警报时关闭?

希望这对您有所帮助!

关于ios - 为什么这个 UIAlertController 不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39008085/

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