简体   繁体   中英

segue performed before the UIAlertController in swift

I have just coded a signup page in which if user does not enter text in any of the uitextfield then it should receive an error message (UIAlert) ; else (if user enter text in all uitextfield) signup page will present for Firebase Authentication.

in my code, user will direct to sign-in page only if authentication successfully complete else it should remain in signup page with alert message.

Problem - my code is able to produce alert message but same time it is taking user to sign-in page automatically even when there is an error. which means it is performing a segue that takes user to sign-in page ie segue unwinding irrespective of alert message.

can anyone help me why this may be happening?

 @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!,])
                        }
            }
        }

}

From this bit of code it doesn't really look like anything is wrong. However, you should block the UI while Auth.auth().createUser(...) is running. Otherwise there's a chance that you call registerPressed with everything correct, but then delete the text from a label and call it again before the callback. That way you have an alert and then the segue is called.

You're also doing something quite crazy with the way that you're presenting your alerts. Instead of creating a new window, adding to it a view controller and all that jazz, just call self.present(alertController, animated: true) . Eg

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)

删除序列并以编程方式推送到目标视图控制器。

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

I was doing a silly mistake where I created the segue directly on IBAction and hence whenever I was pressing the button it was performing the segue irrespective UIAlerts. my updated code is below :

   @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!,])
                        }
            }
        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM