window 尝试呈现 AlertController 时的层次结构消息

window hierarchy message when trying to present an AlertController

我有一个 swift 文件用于管理与 Firebase 的连接(注册、登录、保存数据)。在此文件中,使用一些 AlertController 根据来自 Firebase 的消息通知用户(错误的电子邮件格式,已使用的电子邮件)。问题是当某些 alerController 必须出现时,他们会收到此消息:

Warning: Attempt to present *** on *** whose view is not in the window hierarchy!

A Signup ViewController 是当前视图,此视图链接到另一个特定文件。我在 Whosebug 上的 "windows hierarchy" 和 Google 上阅读了很多内容,并尝试了不同的东西。

我在注册时使用了类似的代码时遇到了同样的问题。我从这种方式开始展示 alertController :

present(alerInvalidEmail, animated: true, completion: nil)

给那个:

UIApplication.shared.keyWindow?.rootViewController?.present(alertInvalidEmail, animated: true, completion: nil)

现在它工作正常,但它不适用于我的注册部分。 我想这与当前ViewController有关,但找不到我的方法。

这是我来自网络文件的代码:

struct NetworkingService {

var databaseRef: FIRDatabaseReference! {
    return FIRDatabase.database().reference()
}
var  storageRef: FIRStorageReference! {
    return FIRStorage.storage().reference()
}


func signUp(email: String, pseudo:String, password: String, data: NSData!){

    FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in

        if error != nil {
            print(error!.localizedDescription)


        if let SignUperrCode = FIRAuthErrorCode(rawValue: error!._code) {
            switch SignUperrCode {

            case .errorCodeInvalidEmail:
                let alertInvalidEmail =  UIAlertController(title: "Email validation", message: "You have entered an malformed adress", preferredStyle: .alert)
                alertInvalidEmail.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction) in
                    print("Invalid email")
                }))

                UIApplication.shared.keyWindow?.rootViewController?.present(alertInvalidEmail, animated: true, completion: nil)


            case .errorCodeEmailAlreadyInUse:
                let alertUsedEmail =  UIAlertController(title: "Email validation", message: "This email is already used", preferredStyle: .alert)
                alertUsedEmail.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction) in
                    print("In use")
                }))
                    UIApplication.shared.keyWindow?.rootViewController?.present(alertUsedEmail, animated: true, completion: nil)


            default:
                print("Create User Error: \(error!)")
            }}

        } else {
            self.setUserInfo(user: user, pseudo : pseudo, password: password, data: data)


            let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "nextView")

            (UIApplication.shared.keyWindow?.rootViewController?.present(vc, animated: true, completion: nil))!
            print("user signed up successfully")

        }
    })
}

这里是用于从 ViewController 文件调用注册函数的代码。

@IBAction func signUpAction(_ sender: Any) {

    let data = UIImageJPEGRepresentation(self.UserImageView.image!, 0.8)

    networkingService.signUp(email: emailTextField.text!, pseudo: pseudoTextField.text!, password: passwordTextField.text!, data: data! as NSData)

也许我不在右边window,但同样的代码在注册部分工作得很好。

在此先感谢您的帮助。

在函数 viewController 中添加一个类型为 UIViewController

的新参数
func signUp(email: String, pseudo:String, password: String, data: NSData!,viewController: UIViewController){

        FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in

            if error != nil {
                print(error!.localizedDescription)


            if let SignUperrCode = FIRAuthErrorCode(rawValue: error!._code) {
                switch SignUperrCode {

                case .errorCodeInvalidEmail:
                    let alertInvalidEmail =  UIAlertController(title: "Email validation", message: "You have entered an malformed adress", preferredStyle: .alert)
                    alertInvalidEmail.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction) in
                        print("Invalid email")
                    }))

                    viewController.present(alertInvalidEmail, animated: true, completion: nil)


                case .errorCodeEmailAlreadyInUse:
                    let alertUsedEmail =  UIAlertController(title: "Email validation", message: "This email is already used", preferredStyle: .alert)
                    alertUsedEmail.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action:UIAlertAction) in
                        print("In use")
                    }))
                        viewController.present(alertUsedEmail, animated: true, completion: nil)


                default:
                    print("Create User Error: \(error!)")
                }}

            } else {
                self.setUserInfo(user: user, pseudo : pseudo, password: password, data: data)


                let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "nextView")

                (UIApplication.shared.keyWindow?.rootViewController?.present(vc, animated: true, completion: nil))!
                print("user signed up successfully")

            }
        })
    }

并将其命名为

 networkingService.signUp(email: emailTextField.text!, pseudo: pseudoTextField.text!, password: passwordTextField.text!, data: data! as NSData, viewController: self)