【发布时间】:2017-11-04 14:34:48
【问题描述】:
我在 Swift 3 中遇到以下错误:
Attempt to present alertcontroller whose view is not in window hierarchy
我已经参考了有关此主题的其他帖子,但无济于事。更具体地说,我已经实现了这篇文章中建议的更改:AlertController is not in the window hierarchy
我仍然收到同样的错误。
我所做的是:
-
创建一个 AlertHelper.swift 助手类:
class AlertHelper {func showAlert(fromController 控制器: UIViewController) { let alert = UIAlertController(title: "Please Try Again", message: "Email Already In Use", preferredStyle: .alert) controller.present(警报,动画:真,完成:无) } }
-
在我的视图控制器中,在按下按钮时调用的函数下,我检查用户输入的电子邮件地址是否已存储在我的 Firebase 数据库中,如果是,我尝试显示“来自 AlertHelper 类的电子邮件已在使用中的警报:
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in if error != nil { if let errCode = AuthErrorCode(rawValue: error!._code) { switch errCode { case .emailAlreadyInUse: let alert = AlertHelper() alert.showAlert(fromController: self) return default: print("other error") } } } else { // Inputs user email into database self.ref.child("Users").child((user?.uid)!).setValue(["Email": email, "Location": ""]) self.performSegue(withIdentifier: "todorm", sender: self) } }
数据库的东西有效,唯一没有出现的是警报。有任何想法吗?谢谢!!
【问题讨论】:
标签: xcode uiviewcontroller swift3 uialertcontroller