在验证失败后,提示用户再次通过UIAlertController输入输入,会在日志中显示警告

时间:2015-12-17 02:18:45

标签: ios swift uialertcontroller

我使用以下功能提示用户输入电子邮件地址。 如果验证失败,则validateEmailAddress和validateVerificationCodeAgainstEmail再次调用下面的函数promptUserToEnterEmail。问题是当再次提示用户时,我收到如下所示的警告消息。

func promptUserToEnterEmail() {

            var alertController:UIAlertController?
            alertController = UIAlertController(title: "Enter Email Address",
            message: "Please enter your email address.",
            preferredStyle: .Alert)

            alertController!.addTextFieldWithConfigurationHandler(
                {(textField: UITextField!) in
                textField.placeholder = "Email"
            })

            let action = UIAlertAction(title: "Submit",
                style: UIAlertActionStyle.Default,
                handler: {[weak self]
                (paramAction:UIAlertAction!) in
                if let textFields = alertController?.textFields{
                    let theTextFields = textFields as [UITextField]
                    let enteredText:UITextField = theTextFields[0]
                    print("prompted email: \(enteredText.text)")
                    // Validate email address
                    self!.validateEmailAddress(enteredText)
                    print("Returned from validateEmailAddress()");
                    // Continue to validate verification code against email address
                    self!.validateVerificationCodeAgainstEmail()
                    print("Returned from validateVerificationCodeAgainstEmail()");
                }
            })

            alertController?.addAction(action)

            let cancelItem = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
                alertController?.addAction(cancelItem)

            self.presentViewController(alertController!,
                animated: true,
                completion: nil)

    }
WARNING MESSAGE:
    Warning: Attempt to present <UIAlertController: 0x7fe6f0eb4bb0> 
on <XXXXXV1.ViewController: 0x7fe6f0cd7400> which is already presenting 
<UIAlertController: 0x7fe6f0e3ea50>

1 个答案:

答案 0 :(得分:0)

如果您对接着显示两个警报感到沮丧,那么您可以在显示第一个警报后延迟一定时间延迟第二个警报:

let delay = 10.0 * Double(NSEC_PER_SEC);
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay));
dispatch_after(time, dispatch_get_main_queue(), {
            //display the second alert here
        })

或者您可以在提交错误后解除警报控制器。 因此,您可以显示第一个警报,并在按下提交按钮后调用firstAlert:

firstAlert.dismissViewControllerAnimated(true, completion: nil)

完成后,只需按照当前的流程