0

I'm getting the following error in Swift 3:

Attempt to present alertcontroller whose view is not in window hierarchy

I have already referred to other posts on this topic to no avail. More specifically, I've implemented the change suggested in this post: AlertController is not in the window hierarchy

and I'm still receiving the same error.

What I've done is:

  1. Create an AlertHelper.swift helper class:

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

  2. In my View Controller, under a function which is called when a button is pressed, I check to see if an email address entered by a user is already stored in my Firebase database, and if so, I try to present an "Email already in use" alert from the AlertHelper class:

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

The database stuff works, the only thing that isn't appearing is the alert. Any ideas? Thanks!!

M. Ying
  • 197
  • 2
  • 15

1 Answers1

0

It's sort of a strange practice to subclass your alert into a separate class.

You should try making a method in your ViewController class

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

Call the method using:

self.showAlert()
Eli Whittle
  • 1,084
  • 1
  • 15
  • 19
  • I've tried that method too, and I still get the same UIAlertController not in window hierarchy error. Here's the strange thing: when I try to present an alert in a simple if-else statement, it works. However, when I try to present the alert in an if statement nested within another function call (as shown above), I get the error. – M. Ying Jun 04 '17 at 00:01
  • Check if the createUser method creates a controller instance. It is possible that it presents a new controller on top of the existing controller so when you try to present on your existing controller, the error occurs. – Eli Whittle Jun 04 '17 at 00:12
  • I suspect that this is the case. If so, how would I go about fixing it? – M. Ying Jun 04 '17 at 00:30