Attempt to present something that is not in windows hierachy

hi everyone, I am facing some issues with running an app on my device. It is a game that requires the player to click on a “Hit Me” button. However, an error pops up in the print section:

how do I solve this? when I run the app on my phone and I click “Hit Me”, the app refreshes and the below error is printed out.

"Attempt to present <UIAlertController: 0x7fb56d826c00> on <bulls_eye.ViewController: 0x7fb56d721ef0> whose view is not in the window hierarchy!

Please help, thank you

Hi @flora_land, could you post a snippet of your code with the alert controller so we can better understand how to help you?


Hi @flora_land,
The error that you mentioned is the most common error that developers come across, in fact if you mention portions of the message, it is evidently clear on the problem.

Here’s what happens,
self.presentViewController(newVC ... generally means present the newVC in the hierarchy so that when you dismiss the viewController, it should revert back to the one you called from. In your case, you are presenting UIAlertController on bulls_eye and I believe you are trying this in viewDidLoad or before the bulls_eye viewController is displayed and part of the view hierarchy.

There are a couple of ways to manage that, one is to display the alert in a delay loop after 2-5 seconds by which the VC would have been part of the view hierarchy.

The other is to use the rootViewController as the parent of the alert than using self.



This topic was automatically closed after 166 days. New replies are no longer allowed.