我从我的添加栏按钮项目调用下面的代码,在这里我显示警告视图,询问用户的输入。它第一次运行良好,并在此之后给出了以下错误:
代码:
var alert = UIAlertController(title: "Enter Blog Link", message: nil, preferredStyle: .Alert)
func userBlogLinkEntryPopover() {
// let alert = UIAlertView(title: "Enter Blog Link", message: nil, delegate: self, cancelButtonTitle: "Cancel")
alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
textField.placeholder = "Enter Blog URL!"
}
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
if let tf = self.alert.textFields?.first as? UITextField{
println(tf.text)
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
presentViewController(alert, animated: true, completion: nil)
}错误:“UIAlertController只能有一个具有UIAlertActionStyleCancel样式的操作”
我认为,每次按下“添加”按钮时,它都会尝试添加操作,从而导致错误。如果我错了,请纠正我,也请指点周围的工作。
谢谢你的帮助。
发布于 2017-11-05 00:07:32
如果你这样做,你也会得到这个,仔细阅读,看看你是否能看到错误。
resendAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
//cancel clicked
return
}))
resendAlert.addAction(UIAlertAction(title: "Check Server", style: .cancel, handler: { (action: UIAlertAction!) in
//check server clicked
return
}))注意-> .cancel是而不是,要使用两次,第二次需要是.default。
resendAlert.addAction(UIAlertAction(title: "Check Server", style: .default, handler: { (action: UIAlertAction!) in
//check server clicked
return
}))工作很好
发布于 2015-04-12 17:56:45
我发现,由于在函数之外声明了警报,所以它保留了所有操作,并因此引发异常。我更正了我的代码如下,它工作得很好。
func userBlogLinkEntryPopover() {
var alert = UIAlertController(title: "Enter Blog Link", message: nil, preferredStyle: .Alert)
alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
textField.placeholder = "Enter Blog URL!"
}
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
if let tf = alert.textFields?.first as? UITextField{
println(tf.text)
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
presentViewController(alert, animated: true, completion: nil)
}问候
发布于 2018-08-14 14:13:27
这是我被绊倒的原因-一个自动填充的错误。使用ObjC,而不是在操作中使用样式UIAlertActionStyleDefault,而是使用UIAlertControllerStyleAlert常量。没有警告,但这两种解析的枚举值为1,因此我无意中向同一个控制器添加了两个取消操作。
https://stackoverflow.com/questions/29592522
复制相似问题