我在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 controller:UIViewController){let alert = UIAlertController(title:“Please Try Again”,message:“Email Already In Use”,preferredStyle:.alert)controller.present(alert,animated:true,completion:nil) }}
- 在我的视图控制器中,在按下按钮时调用的功能下,我检查用户输入的电子邮件地址是否已存储在我的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)
}
}
数据库的东西是有效的,唯一没有出现的是警报 . 有任何想法吗?谢谢!!
1 回答
将警报子类化为单独的类是一种奇怪的做法 .
您应该尝试在ViewController类中创建一个方法
使用以下方法调用方法