2016-07-28 8 views
0

は表示されません。警告ポップアップは、私は私のプロジェクトでいくつかのコードを持っている

@IBAction func createAccountAction(sender: AnyObject) { 
    if self.emailField.text == "" || self.passwordField.text == "" 
    { 
     let alertController = UIAlertController(title: "Oops!", message: "Please enter an email and password.", preferredStyle: .Alert) 

     let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil) 
     alertController.addAction(defaultAction) 

     self.presentViewController(alertController, animated: true, completion: nil) 
    } 
    else 
    { 
     FIRAuth.auth()?.createUserWithEmail(self.emailField.text!, password: self.passwordField.text!) { (user, error) in 

      if error == nil 
      { 
       let alertController = UIAlertController(title: "Done", message: "Account created!", preferredStyle: .Alert) 

       let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil) 
       alertController.addAction(defaultAction) 
       self.emailField.text = "" 
       self.passwordField.text = "" 



      } 
      else 
      { 
       let alertController = UIAlertController(title: "Oops!", message: error?.localizedDescription, preferredStyle: .Alert) 

       let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil) 
       alertController.addAction(defaultAction) 

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

あなたは私がcreateAccountActionをクリックしたときに表示されるメッセージOopsでalertControllerを持って、最後のelseステートメントで見ることができるようにボタン

しかし、ユーザーがボタンを押して、テキストフィールドに何も入力していない場合に起こります。

私が望むのは、ユーザーがテキストフィールドを正常に入力すると、指定した他のテキストとともに同じポップアップが表示されることです。

私はそれが一部

self.emailField.text = "" 
self.passwordField.text = "" 

を行いますがAlertControllerを提示していないコードを実行します。

私は何をしたいのですか?

答えて

0

あなたがライン行方不明:

はさらにそのようなバグが起こらないようにする例をリファクタリング

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

する必要があります後:

self.emailField.text = "" 
self.passwordField.text = "" 

だからあなたは警告を提示していません。

また、その機能全体をこれよりはるかに単純なものに変更することができます:

@IBAction func createAccountAction(sender: AnyObject) { 

    if self.emailField.text == "" || self.passwordField.text == "" { 

     let title = "Oops" 
     let message = "Please enter an email and password." 

     let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert) 
     let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil) 
     alertController.addAction(defaultAction) 
     self.presentViewController(alertController, animated: true, completion: nil) 

    } else { 
     FIRAuth.auth()?.createUserWithEmail(self.emailField.text!, password: self.passwordField.text!) { (user, error) in 

      var title : String 
      var message : String 

      if error == nil { 
       title = "Done" 
       message = "Account created!" 
       self.emailField.text = "" 
       self.passwordField.text = "" 
      } else { 
       title = "Oops!" 
       message = "error?.localizedDescription" 
      } 

     let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert) 
     let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil) 
     alertController.addAction(defaultAction) 
     self.presentViewController(alertController, animated: true, completion: nil) 

     } 
    } 
} 
+0

ありがとうございます!それはそれを修正する...深夜コーディングは、あなたがそのような小さなシンプルなものを監督する原因になります..:P – matthewdossantos

+0

あなたは私の答えに満足している場合は、チャンスを持っているときに横のチェックをクリックしてください。 – WMios

+1

10分後に一度やります:) – matthewdossantos

0

'if else'ステートメントの 'else'部分にアラートコントローラのみを表示しています。

var title: String! 
    var message: String! 

    if let error = error { 
     title = "Oops!" 
     message = error.localizedDescription 

    } else { 
     title = "Done" 
     message = "Account created!" 

     self.emailField.text = "" 
     self.passwordField.text = "" 
    } 

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert) 
    let defaultAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil) 
    alertController.addAction(defaultAction) 
    self.presentViewController(alertController, animated: true, completion: nil) 
関連する問題