2016-12-02 107 views
-1

我正在創建一個應用程序,當用戶將文本字段留空或不會使兩個密碼匹配時,將顯示錯誤警報。如何關閉彈出錯誤消息

問題是當信息正確並且兩個密碼匹配時,錯誤警報仍在呈現。

的代碼可以看到下面:

//creating an action that will check when the sign up button is selected. 

@IBAction func registerButtonTapped(_ sender: Any) { 

    let userEmail = userEmailTextField.text; 
    let userPassword = userPasswordTextField.text; 
    let userRepeatPassword = repeatPasswordTextField.text; 

    // if one of the fields is empty the user must repeat the password 
    if ((userEmail?.isEmpty)! || (userPassword?.isEmpty)! || (userRepeatPassword != nil)) { 

     //this is the alert pop up shown to the user. 
     let alertController = UIAlertController(
      title: "Error!!!!!!", 
      message: "Something Went Wrong, Try Again", 
      preferredStyle: UIAlertControllerStyle.alert 
     ) 

     let cancelAction = UIAlertAction(
      title: "Cancel", 
      style: UIAlertActionStyle.destructive) { (action) in 

     } 

     let confirmAction = UIAlertAction(
     title: "OK", style: UIAlertActionStyle.default) { (action) in 

     } 

     alertController.addAction(confirmAction) 
     alertController.addAction(cancelAction) 

     self.present(alertController, animated: true){() -> Void in 
      return; 
     } 
    } 

func registerButtonTapped(_ sender: Any) { 

    //check if password and repeat password are the same 
    if (userPassword != userRepeatPassword) { 

     //display an alert message, user will not be able to continue // not too sure if this works 
     let alertController = UIAlertController(
      title: "Error!!!!!!", 
      message: "Something Went Wrong Try Again", 
      preferredStyle: UIAlertControllerStyle.alert 
     ) 

     let cancelAction = UIAlertAction(
      title: "Cancel", 
      style: UIAlertActionStyle.destructive) { (action) in 

     } 

     let confirmAction = UIAlertAction(
      title: "OK", 
      style: UIAlertActionStyle.default) { (action) in 

     } 

     alertController.addAction(confirmAction) 
     alertController.addAction(cancelAction) 

     self.present(alertController, animated: true){() -> Void in 
      return; 
     } 

當正確的信息已經輸入的用戶應該能夠看到一個確認,說明已創建的帳戶。

//display prompt message (confirmation) 

//this will show a message to the user showing that the registration has been successful this is not working 
func showAlert() { 
    let alertController = UIAlertController(title: "Thank You", message: "Registration Has Been Completed. Thank You", preferredStyle: .alert)  

    let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) 

    alertController.addAction(defaultAction) 

    self.present(alertController, animated: true){() -> Void in 
     return; 
    } 
} 

這也沒有顯示出來。

+1

歡迎來到SO!請不要在人們面前喋喋不休,並閱讀如何提出一個好問題:http://stackoverflow.com/help/how-to-ask – shallowThought

+0

showAlert在哪裏被調用? –

+0

吶喊助威,吶喊助威。 – halfer

回答

0

我寫了關於如何解決這個問題的全部內容。您可以從這裏參考

@IBOutlet var userNameField: UITextField! 
@IBOutlet var passwordfield: UITextField! 
@IBOutlet var confirmPasswordField: UITextField! 

@IBAction func registerButtonTapped() { 

    if userNameField.text == "" && passwordfield.text == "" && confirmPasswordField.text == "" { 
     alertController(title: "Error", message: "One or more fields are missing", cancelTitle: "Try again!") 
    } else { 

     if passwordfield.text != confirmPasswordField.text { 
      alertController(title: "Error", message: "Your passwords do not match", cancelTitle: "Try again!") 
     } else { 
      //switch views, or do whatever you want when the user correctly enters in their information 
     } 
    } 

} 

func alertController(title: String, message: String, cancelTitle: String) { 


    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) 
    let cancelAction = UIAlertAction(title: cancelTitle, style: .cancel) 

    alertController.addAction(cancelAction) 
    self.present(alertController, animated: true, completion: nil) 
} 
+0

嗨西里爾感謝您的意見,我已經提出了所有的建議,你已經陳述和某些原因,當所有的字段和密碼輸入正確時,它仍然顯示錯誤信息。 –

+0

嗨,我還有一個額外的錯誤,說線程1信號SIGABRIT –

+0

我應該仔細閱讀你的問題。我會回答一個我到達電腦的問題。 –