2017-12-25 29 views
0

사용자가 uitextfield에 텍스트를 입력하지 않으면 오류 메시지 (UIAlert)가 수신되는 가입 페이지를 코딩했습니다. 그렇지 않으면 (사용자가 모든 uitextfield에 텍스트를 입력하면) 가입 페이지가 Firebase 인증에 표시됩니다.신속히 UIAlertController 이전에 segue가 수행되었습니다.

내 코드에서 사용자는 인증이 성공적으로 완료된 경우에만 로그인 페이지로 이동하고 그렇지 않은 경우 알림 메시지가있는 가입 페이지에 남아 있어야합니다.

문제 - 내 코드는 경고 메시지를 생성 할 수 있지만 오류가 발생해도 사용자가 로그인 페이지를 자동으로 가져 오는 것과 같은 시간입니다. 이는 사용자가 로그인 페이지, 즉 경고 메시지에 관계없이 방향을 풀어주는 세그 (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으로 모든 사항을 올바르게 입력 한 다음 레이블에서 텍스트를 삭제하고 콜백 전에 다시 호출 할 수 있습니다. 그런 식으로 경고하고 다음 segue가 호출됩니다.

당신은 또한 당신이 당신의 경고를 제시하는 방식으로 아주 미친 짓을하고 있습니다. 새 윈도우를 만드는 대신보기 컨트롤러와 모든 재즈를 추가하여 self.present(alertController, animated: true)으로 호출하면됩니다. 예 : I는 I 직접 IBAction를 I에 따라서는 그것이 SEGUE 무관 UIAlerts을 수행 한 버튼을 누를 때마다 하였다 SEGUE 생성 바보 같은 실수를 수행 하였다

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 버튼에 직접 segue를 만들었습니다. 버튼을 누를 때마다 아무 것도 상관없이 segue를 수행하고있었습니다 ... segue를 삭제하고 로그인보기 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!,]) 
         } 
      } 
     } 

}