2016-10-14 77 views
0

我在故事板中创建了一个按钮。并为它创建了一个行动和出路。我做了一些按钮操作。但按钮操作仅在第二次执行。第一次点击按钮动作不起作用

这里是我的按钮动作代码:

@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! 

附:我在这里使用Swift 2.3。其实,我想把按钮的标题设置为新的。这是第二次敲击后发生的变化。

回答

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) 
} 
+0

我忘了说什么,我试图做的,对不起为此:)我想setTitle到新的。我将编辑我的问题 –

+0

这对我来说确实有效。谢谢!你能解释一下如何帮助吗? –