ios - 名称文本字段成为强制性的然后 UIAlert 停止工作

标签 ios swift firebase firebase-authentication

我添加了一行代码,以便在使用 firebase 注册帐户时名称文本字段是强制性的,但是当我这样做时,UIAlert 崩溃了。当我添加那行代码时它就停止显示了。我添加的代码用 > 突出显示。解决问题的最佳方法是什么?重新编码强制名称文本字段或重新编码 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)
                }
            })
        }
    }
}

最佳答案

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

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

试试这个!

关于ios - 名称文本字段成为强制性的然后 UIAlert 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45369678/

相关文章:

ios - 我没有在 pod 'DropDown' 中得到我选择的值

swift - 如何在 firebase 数据中显示嵌套对象

swift - fatal error : Unresolved error Error Domain=NSCocoaErrorDomain Code=134140

python - 如何以编程方式创建 Google Cloud 存储访问 token python

ios - Swift 3 中没有这样的模块 'GoogleMobileAds'

IOS Model View Controller 通信,用于从弹出窗口中的 slider 实时更新

iphone - 如何将新的 viewController 推送到另一个 View Controller 中的现有 View 中?

ios - UIViewController, Storyboard,没有 init 方法

ios - 条件 IBAction(如果生命 == 1 显示 "Buy Extra Lives"按钮)

javascript - 删除在 React 和 Firebase 中不起作用