UIAlertController错误在swift中的模态呈现视图控制器中调用时

时间:2015-08-20 07:02:13

标签: ios swift

我有一个模态呈现的视图控制器,当我运行UIAlertController时,不断收到错误消息。我该如何解决这个问题。

警报是从一个检查文本字段为空的按钮触发的。

@IBAction func registerAction(sender: AnyObject) {
if(userEmail.isEmpty){
        alertMessage("Fields Are Empty")
    }
}



func alertMessage(userMessage:String){  
    var myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
    myAlert.addAction(okAction)
    self.presentViewController(myAlert, animated: true, completion: nil)
}

运行警报时出错

Warning: Attempt to present <UIAlertController: 0x7f9bbb9060d0>  on <AlertApp.CustomSignupVC: 0x7f9bb94accb0> which is already presenting <UIAlertController: 0x7f9bbb926660>

1 个答案:

答案 0 :(得分:2)

尝试将其添加到主操作队列中。

dispatch_async(dispatch_get_main_queue()) {
    self.presentViewController(myAlert, animated: true, completion: nil)
}

检查此相关问题:Presenting a view controller modally from an action sheet's delegate in iOS8 - iOS10