简体   繁体   English

迅速在UIAlertController之前执行segue

[英]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) ; 我刚刚编写了一个注册页面,其中如果用户未在任何uitextfield中输入文本,则它应该收到一条错误消息(UIAlert); else (if user enter text in all uitextfield) signup page will present for Firebase Authentication. 否则(如果用户在所有uitextfield中输入文本)将显示Firebase身份验证的注册页面。

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. 这意味着它正在执行segue,使用户登录页面,即不管警报消息如何,segue都将平仓。

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. 但是,您应该在Auth.auth().createUser(...)运行时阻止UI。 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. 否则,您有可能在一切正确的情况下调用registerPressed ,但是随后从标签中删除了文本并在回调之前再次调用它。 That way you have an alert and then the segue is called. 这样,您将收到警报, 然后调用segue。

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) . 无需创建新窗口,而是向其添加视图控制器和所有爵士乐,只需调用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. 我犯了一个愚蠢的错误,我直接在IBAction上创建了segue,因此每当我按下按钮时,它都会执行segue,而与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!,])
                        }
            }
        }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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