按钮操作第一次点击时不起作用

时间:2016-10-14 22:13:55

标签: ios swift uibutton

我在故事板中创建了一个按钮。并为它创造了一个动作和出路。我在一个按钮的操作中执行一些任务。但是按钮动作仅在第二次执行。

这是按钮操作的代码:

@IBAction func sendButtonTapped(sender: UIButton) {

        let domain = String(txtEmail.text!.characters.suffix(9))

        if txtEmail.text == "" || domain != "nu.edu.kz" {
            let alertController = UIAlertController(title: "Error", message: "Please enter your valid nu.edu.kz e-mail", preferredStyle: .Alert)
            let defaultAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
            alertController.addAction(defaultAction)

            self.presentViewController(alertController, animated: true, completion: nil)
        } else {

            if sendButton.currentTitle != "Reset Password has been sent" {

                FIRAuth.auth()?.sendPasswordResetWithEmail(self.txtEmail.text!, completion: { (error) in

                    print(error?.localizedDescription)

                    if error != nil {

                        print(error?.localizedDescription)
                    } else {

                        print("reset password email has been sent")

                        self.sendButton.setTitle("Reset Password has been sent", forState: .Normal)

                    }
                })
            }
        }
    }

正如我之前所说,我为我的按钮和文本字段创建了一个插座:

@IBOutlet weak var sendButton: UIButton!
@IBOutlet weak var txtEmail: UITextField!

P.S。我在这里使用Swift 2.3。实际上,我想将按钮的标题设置为新按钮。这是第二次敲击后的变化。

1 个答案:

答案 0 :(得分:1)

请参阅:

presentViewController:animated:YES view will not appear until user taps again

可能你想要做的就是致电presentViewController(alertController, animated: true, completion: nil)  显式主线程

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