Anuar  Maratkhan Anuar Maratkhan - 1 year ago 62
Swift Question

button action does not work for the first time tapped

I have created a button in my storyboard. And created an action and outlet for it. I do some tasks in action of a button. But the button action is performed only from the second time.

Here's my code for the button action:

@IBAction func sendButtonTapped(sender: UIButton) {

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

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

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


if error != nil {

} else {

print("reset password email has been sent")

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


And as I said before I have created an outlet for my button and textfield:

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

P.S. I use Swift 2.3 here. Actually, I want to setTitle of the button to the new one. And this changes after tapping for the second time.

Answer Source

Please refer to this:

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

Probably want you want to do is call presentViewController(alertController, animated: true, completion: nil) explicitly on main thread

dispatch_async(dispatch_get_main_queue()) {
     self.presentViewController(alertController, animated: true, completion: nil)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download