命名文本字段为强制性,然后UIAlert停止工作

时间:2017-07-28 09:22:48

标签: ios swift firebase firebase-authentication

我添加了一行代码,以便在使用firebase注册帐户时必须使用name text-field,但是当我这样做时,UIAlert就破坏了。当我添加那行代码时,它停止显示。我添加的代码以>突出显示。解决问题的最佳方法是什么?重新编码强制名称text-field或重新编码UIAlert。哪种方法最简单?

        @IBAction func registerTapped(_ sender: Any) {
        let namec = nameTextField.text
         if let email = emailTextField.text, let pass = passwordTextField.text, let name = (namec?.capitalized.isEmpty)! ? nil:namec?.capitalized {
            FIRAuth.auth()?.createUser(withEmail: email, password: pass, completion: { (user, error) in
                if user != nil {
                    //user found

                    let interval = NSDate().timeIntervalSince1970
                    let date = Date(timeIntervalSince1970: interval)
                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "dd/MM/yyyy/HH/mm/SS"
                    // you can change the date format to whatever you wants
                    let dateString = dateFormatter.string(from: date)
                    print(dateString)
                    self.refD?.child("Users").child((user?.uid)!).setValue(["Email": email, "Name": name, "User Created": dateString])
                    print("User Created And Added To Database", email, name, dateString)
                    self.performSegue(withIdentifier: "registertologin", sender: self)
                }
                else {
                    print(error!)
                    let alert = UIAlertController(title: "Error Creating Account ", message: "\(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.alert)
                    alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
                    self.present(alert, animated: true, completion: nil)
                }
            })
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为您必须在主队列块中添加警报代码,因为您的代码位于完成处理程序块

DispatchQueue.main.async {
    print(error!)
    let alert = UIAlertController(title: "Error Creating Account ", message: "\(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
    self.present(alert, animated: true, completion: nil)
}

试试这个!