我编写了代码,当我的一个 UITextFields 中的输入小于 1050 时出现警报。当输入满足时它成功出现,但在我按下“确定”后它立即重新出现。
下面是viewDidLoad
函数中的代码:
override func viewDidLoad(){
super.viewDidLoad()
alert = UIAlertController(title: "Error", message: "Please enter an exit width value greater than 1050", preferredStyle: UIAlertControllerStyle.Alert)
let okay = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler: valueCalc)
alert.addAction(okay)
}
然后我的 valueCalc
函数(点击按钮时调用):
@IBAction func valueCalc(sender: AnyObject){
if(Int(mmText.text!)! < 1050){ //mmText is an UITextField
self.presentViewController(alert, animated: true, completion: nil)
}
}
最佳答案
根据你的代码行
let okay = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler: valueCalc)
当您按下 OK 时,将调用您的处理程序名称 valueCalc。
再次计算出小于指定字符的值,向您显示警报。
取而代之的是,在您的代码中替换这一行 -
let okay = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler: handlerMethod)
并将此方法添加到您的代码中
func handlerMethod() {
//handle your action here after ok is pressed for e.g if you wanna just dismiss the alert then write
dismissViewControllerAnimated(true, completion: nil)
}
关于ios - UIAlertController 在关闭后一直重新出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38685923/