Dan*_*ger 0 iphone alert ios swift uialertcontroller
我已经写代码,出现报警时,在我的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)
}
Run Code Online (Sandbox Code Playgroud)
然后我有我的valueCalc功能(当点击按钮时调用):
@IBAction func valueCalc(sender: AnyObject){
if(Int(mmText.text!)! < 1050){ //mmText is an UITextField
self.presentViewController(alert, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
根据你的代码行
let okay = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler: valueCalc)
Run Code Online (Sandbox Code Playgroud)
按OK后,将调用您的处理程序名称valueCalc.
再次计算该值,当出现的值小于指定的字符时,会显示警报.
而不是那样,在代码中替换这一行 -
let okay = UIAlertAction(title: "OK", style: UIAlertActionStyle.Destructive, handler: handlerMethod)
Run Code Online (Sandbox Code Playgroud)
并将此方法添加到您的代码中
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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
116 次 |
| 最近记录: |