尝试显示其视图不在窗口层次结构中的alertcontroller

时间:2017-06-03 07:28:11

标签: xcode uiviewcontroller swift3 uialertcontroller

我在Swift 3中遇到以下错误:

Attempt to present alertcontroller whose view is not in window hierarchy

我已经提到关于这个主题的其他帖子无济于事。更具体地说,我已实施了此帖中建议的更改:AlertController is not in the window hierarchy

我仍然收到同样的错误。

我所做的是:

  1. 创建一个AlertHelper.swift助手类:

    class AlertHelper {
    

    func showAlert(fromController controller:UIViewController){     let alert = UIAlertController(标题:“请再试一次”,消息:“电子邮件已在使用中”,preferredStyle:.alert)     controller.present(alert,animated:true,completion:nil) } }

  2. 在我的View Controller中,在按下按钮时调用的功能下,我检查用户输入的电子邮件地址是否已存储在我的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)
            }
        }
    
  3. 数据库的工作原理,唯一没有出现的是警报。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

将警报子类化为单独的类是一种奇怪的做法。

您应该尝试在ViewController类中创建一个方法

func showAlert() { 
   let alert = UIAlertController(title: "Please Try Again", message: "Email Already In Use", preferredStyle: .alert) 
   self.present(alert, animated: true, completion: nil)
}

使用以下方法调用方法:

self.showAlert()
相关问题