在Swift中显示警报的问题(点击“确定”后,它会解开segue

时间:2015-03-12 03:58:52

标签: ios swift uialertcontroller

我在下面编写了以下函数,通过我的项目显示警报。每当我检查表单中的错误时,我会在用户单击提交时显示警报。我想简单地显示警报,但不要放松过去的segue。我想留在当前屏幕上,让用户有机会填写表格。现在,当用户单击提交时,它会显示警告..但是当我单击确定按钮以关闭警报时,它会立即将segue展开到上一个屏幕。

我已经在课程中包含了UIAlertViewDelegate ....任何想法为什么会发生这种情况?

func displayAlert(title:String, error:String) {

    var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in

        self.dismissViewControllerAnimated(true, completion: nil)

    }))

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

}

1 个答案:

答案 0 :(得分:0)

没关系。

func displayAlert(title:String,error:String){

var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in
   //comment out this line and it will still work. It will not segue back to the previous screen. 
    //self.dismissViewControllerAnimated(true, completion: nil)

}))

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

}