ios - UIAlertController 在关闭后一直重新出现

标签 ios iphone swift alert 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)
}

然后我的 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/

相关文章:

ios - 仅加载UITableView中的可见单元格

ios - 从远程数据库填充 UITableView 单元格

ios - 一键两个目标

iphone - 如何通过另一个 CGMutalbePathRef 剪辑或减去 CGMutablePathRf?

ios - 为什么我的视频没有显示在我使用 AVPlayer 的 ScrollView 中的单元格中

iphone - UIActivityViewController - 检测发送错误与取消错误

ios - 防止滑动 UIPageViewController 时出现的白色间隙

iphone - 托管对象上下文保存和refreshObject :mergeChanges:有什么区别

ios - 从今天的小部件按钮在应用程序 Controller 上执行功能

ios - 如何在iOS中创建ByteBuffer?