Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
Swift 4 Attempt to present ViewController whose view is not in the window hierarchy

I am using swift 4 and I am trying to create an alertView when I there is an error while signing up a user using Firebase. I have an IBAction for the sign up button which will sign the user up using text from two textfields, one for email and one for password.

I am basically trying to show an alertview when there is an error with the sign up process, for example there is an empty textfield.

enter image description here

I have attached a screenshot of the function to where that is occuring. I know that I am in fact getting an error because the print statement outputs an error if there is one.

Regardless of if there is an error or not, there is no alert view showing up and the app performs the segue regardless.

2019-01-15 21:40:26.368924-0500 Pronto[9036:225268] Warning: Attempt to present on whose view is not in the window hierarchy

This is the output that I am getting for the alertview now showing up. I have looked at all the other posts about this same issue but none seem to work.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

This issue happens due to your view hierarchy.

You need to find out what is your Current/Topmost view controller in view hierarchy and present your alert over it.

To find out topmost view controller use following code:

func getTopMostViewController() -> UIViewController? {
    var topMostViewController = UIApplication.shared.keyWindow?.rootViewController

    while let presentedViewController = topMostViewController?.presentedViewController {
        topMostViewController = presentedViewController
    }

    return topMostViewController
}

And present your alert over topmost view controller and use main thread to present an alert because closures may have working on another thread.

DispatchQueue.main.async { 
    getTopMostViewController()?.present(alertController, animated: true, completion: nil)
}

Please refer to this stack answer: Swift 3 Attempt to present whose view is not in the window hierarchy

over 4 years ago · Santiago Trujillo Report

0

Try using ViewDidAppear instead of View did Load.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let alertViewController = UIAlertController(title: "Any", message: "Any Custom Message", preferredStyle: .alert)

    alertController.addAction(UIAlertAction(title: "ANy", style: .cancel, handler: nil))
    present(alertViewController, animated: true, completion: nil)
}
over 4 years ago · Santiago Trujillo Report

0

You can get the top most view controller and have that view controller present the alert. So instead of self.present use this approach and see if it works:

let topViewController = UIApplication.shared.keyWindow?.rootViewController
topViewController?.present(alertController, animated: true, completion: nil)

Also try presenting on the main thread, since you're trying to show the alert in createUser completion handler:

DispatchQueue.main.async { 
    self.present(alertController, animated: true, completion: nil)
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!