2017-07-28 39 views
0

我添加了一行代码,这样在使用Firebase注册帐户时名称文本字段是强制性的,但是当我这样做时UIAlert破坏了。当我添加该行代码时,它停止显示。我添加的代码用>突出显示。解决问题的最佳方法是什么?重新编码强制名称文本字段或重新编码UIAlert。哪种方法最简单?name textfield made mandatory,然后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

我认为你必须在主队列块添加报警代码,因为你的代码是一个完成处理程序块内

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) 
} 

试试这个!

+0

工作,但必须在名称文本框中的文本弹出之前 –

+0

是的,你只需要把这个东西在你的其他条件 – Rishabh

+0

谢谢我明白了:D –