2016-10-14 111 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

這對我來說確實有效。謝謝!你能解釋一下如何幫助嗎? –