【问题标题】:segue performed before the UIAlertController in swift在 swift 中的 UIAlertController 之前执行 segue
【发布时间】:2017-12-25 15:11:54
【问题描述】:

我刚刚编写了一个注册页面,如果用户没有在任何 uitextfield 中输入文本,那么它应该会收到一条错误消息 (UIAlert) ;否则(如果用户在所有 uitextfield 中输入文本)注册页面将显示 Firebase 身份验证。

在我的代码中,只有在身份验证成功完成后,用户才会定向到登录页面,否则它应该保留在注册页面中并显示警告消息。

问题 - 我的代码能够生成警报消息,但同时即使出现错误,它也会让用户自动登录页面。这意味着它正在执行一个转场,将用户带到登录页面,即转场展开,而不考虑警报消息。

谁能帮助我为什么会发生这种情况?

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

}

【问题讨论】:

    标签: ios swift uialertcontroller ibaction unwind-segue


    【解决方案1】:

    从这段代码来看,看起来并没有什么问题。但是,您应该在 Auth.auth().createUser(...) 运行时阻止 UI。否则,您可能会在一切正确的情况下调用registerPressed,然后从标签中删除文本并在回调之前再次调用它。这样你就有了一个警报,然后调用了 segue。

    您在呈现警报的方式上也做了一些非常疯狂的事情。无需创建新窗口,向其添加视图控制器和所有爵士乐,只需调用self.present(alertController, animated: true)。例如

    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)
    

    【讨论】:

    • 你是绝对正确的,但如果我不使用我的代码的疯狂部分,它会给我一个错误,即尝试呈现 UIAlertController 其视图不在窗口层次结构中!所以它避免了我不得不使用爵士乐的东西的错误..别无选择
    • 这对你来说应该是一个危险信号。如果执行此操作的视图控制器不在窗口层次结构中,则您的设置存在严重问题。你这里的代码不是“爵士乐”,而是隐藏了一个更大问题的 hack。
    • 你是绝对正确的..我犯了一个愚蠢的错误,导致了一切。我直接在 IBAction 按钮上创建了一个转场,所以每当我按下按钮时,它都会执行转场,而不管任何事情.. 我删除了那个转场并创建了一个新的转场,在其中我将注册视图连接到登录 UI 视图,它解决了所有问题
    • 不客气 :) 如果我的回答对您有帮助,请标记它。我需要那些甜蜜的声誉点:p
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      我犯了一个愚蠢的错误,我直接在 IBAction 上创建了 segue,因此每当我按下按钮时,它都会执行 segue 而与 UIAlerts 无关。我更新的代码如下:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多