2017-12-25 28 views
0

ユーザーがuitextフィールドにテキストを入力しないと、エラーメッセージ(UIAlert)を受け取るサインアップページをコーディングしました。 else(ユーザーがすべてのuitextfieldにテキストを入力した場合)サインアップページがFirebase認証用に表示されます。すぐにUIAlertControllerの前でsegueが実行される

私のコードでは、ユーザーは認証が正常に完了した場合にのみサインインページに誘導します。それ以外の場合は、サインアップページにアラートメッセージを残してください。

問題 - 私のコードではアラートメッセージが生成されることがありますが、エラーがあっても自動的にサインインページを表示します。これは、ユーザーがサインインページを取る、すなわちアラートメッセージに関係なくセグを巻き戻すセグを実行していることを意味する。

誰でも私がなぜこのようなことが起こっているのを助けることができますか?

@IBAction func registerPressed(_ sender: Any) { 

    if nameText.text!.isEmpty || genderText.text!.isEmpty || countryText.text!.isEmpty || yourSchool.text!.isEmpty || yourClass.text!.isEmpty { 

     print("Please fill all fields") //my code is printing this error 

     //alert message popup - my code is ble to produce this alert but same it is performing segue and taking user to signin page 
     //ideally, i want user to be in signup page unless all criteria meet 

     let alertController = UIAlertController(title: "Error", message: "Please fill all fields", preferredStyle: .alert) 
     alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action:UIAlertAction) in 
     print("Okay") 
     })) 

     let alertWindow = UIWindow(frame: UIScreen.main.bounds) 
     alertWindow.rootViewController = UIViewController() 
     alertWindow.windowLevel = UIWindowLevelAlert 
     alertWindow.makeKeyAndVisible() 
     alertWindow.rootViewController?.present(alertController, animated: true, completion: nil) 


    } 

    else { 

     Auth.auth().createUser(withEmail: yourEmail.text!, password: yourPassword.text!) { (user, error) in 

        if error != nil { 

         ///print errror message 

         let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert) 
         alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action:UIAlertAction) in 
          print("Okay") 
         })) 

         let alertWindow = UIWindow(frame: UIScreen.main.bounds) 
         alertWindow.rootViewController = UIViewController() 
         alertWindow.windowLevel = UIWindowLevelAlert + 1; 
         alertWindow.makeKeyAndVisible() 
         alertWindow.rootViewController?.present(alertController, animated: true, completion: nil) 

        } 

        else { 

         print("You have successfully signed up") 

         self.performSegue(withIdentifier: "JoinUs2SignPage", sender: self) 

         //updating user information 
         let userID = Auth.auth().currentUser!.uid 
         let usertype: String = "Student" 
         self.ref.child("users").child(userID).setValue(["usertype": usertype ,"username": self.nameText.text!, "usergender": self.genderText.text!, "usercountry": self.countryText.text!, "userschool": self.yourSchool.text!, "userclass": self.yourClass.text!,]) 
         } 
      } 
     } 

} 

答えて

0

segueを削除し、宛先ビューコントローラをプログラムでプッシュします。

self.navigationController?.pushViewController(destinationVC, animated: true) 
1

このコードから、実際には何かが間違っているようには見えません。ただし、Auth.auth().createUser(...)が実行されている間はUIをブロックする必要があります。それ以外の場合は、registerPressedにすべて正しいものを付けてコールする可能性がありますが、ラベルからテキストを削除し、コールバックの前にもう一度コールしてください。あなたはアラートを持っているので、となり、というセグが呼び出されます。

また、アラートを表示しているときに、非常に狂ったこともしています。新しいウィンドウを作成する代わりに、それにビューコントローラとそのすべてのジャズを追加するだけで、self.present(alertController, animated: true)と呼んでください。例えば。私はIBActionに直接セグエを作成した愚かな間違いをしていたので、私はそれがセグエかかわらずUIAlertsを実行してボタンを押した時はいつでも

let alertController = UIAlertController(title: "Error", message: "Please fill all fields", preferredStyle: .alert) 
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action:UIAlertAction) in 
    print("Okay") 
})) 

self.present(alertController, animated: true) 
+0

あなたは絶対的に正しいですが、私は自分のコードの狂気の一部を使用していけない場合、それは私のビューウィンドウ階層にないUIAlertControllerを提示するエラーすなわち試みを与えるだろう! それはエラーを避けるために私はジャジーなものを使用しなければなりませんでした。選択肢なし –

+0

これは赤い旗でなければなりません。このような作業を行っているビューコントローラがウィンドウ階層にない場合は、セットアップに重大な問題があります。ここのコードは「ジャジー」ではなく、大きな問題を隠しているハックです。 –

+0

あなたは絶対に正しいと言われました。私はすべてを引き起こした愚かな間違いをしていました。私はIBActionボタンに直接セグを作成しましたので、ボタンを押すたびにセグを実行していました。セグを削除して、サインアップUIビューにサインアップビューを接続して新しいものを作成し、すべてを解決しました –

0

。私の更新されたコードは以下の通りです:

@IBAction func registerPressed(_ sender: Any) { 

    if nameText.text!.isEmpty || genderText.text!.isEmpty || countryText.text!.isEmpty || yourSchool.text!.isEmpty || yourClass.text!.isEmpty { 

     //alert message popup 

     let alertController = UIAlertController(title: "Error", message: "Please fill all fields", preferredStyle: .alert) 
     alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action:UIAlertAction) in 
     print("Okay") 
     })) 

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


    } 

    else { 

     Auth.auth().createUser(withEmail: yourEmail.text!, password: yourPassword.text!) { (user, error) in 

        if error != nil { 

         ///print errror message 

         let alertController = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: .alert) 
         alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action:UIAlertAction) in 
          print("Okay") 
         })) 

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

        } 

        else { 

         let alertController = UIAlertController(title: "Congratulation", message: "You have successfully signed up", preferredStyle: .alert) 
         alertController.addAction(UIAlertAction(title: "Get Started", style: .default, handler: { (action:UIAlertAction) in 

          self.performSegue(withIdentifier: "back2SignPage", sender: self) 
         })) 

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

         //updating user information 
         let userID = Auth.auth().currentUser!.uid 
         let usertype: String = "Student" 
         self.ref.child("users").child(userID).setValue(["usertype": usertype ,"username": self.nameText.text!, "usergender": self.genderText.text!, "usercountry": self.countryText.text!, "userschool": self.yourSchool.text!, "userclass": self.yourClass.text!,]) 
         } 
      } 
     } 

} 
関連する問題