Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy


📱 How to Present UIViewController on a UIViewController Not in the Window Hierarchy
So, you just upgraded to Xcode 4.5 and encountered the following error in your console:
Warning: Attempt to present
<finishViewController: 0x1e56e0a0>
on<ViewController: 0x1ec3e000>
whose view is not in the window hierarchy!
Don't worry, you're not alone! This error typically occurs when you're trying to present a UIViewController
on another UIViewController
that is not yet part of the window hierarchy. This means that the view you're trying to present the new view on is not visible at the moment.
💡 Understanding the Problem
When presenting a new view controller in iOS, it's important to ensure that the view controller you're trying to present is already part of the window hierarchy. This typically happens after the view controller's view has been loaded and added to the window.
Now, let's take a look at the code you provided:
UIStoryboard *storyboard = self.storyboard;
finishViewController *finished = [storyboard instantiateViewControllerWithIdentifier:@"finishViewController"];
[self presentViewController:finished animated:NO completion:NULL];
The above code seems fine, but the error message suggests that the self
view controller's view is not yet in the window hierarchy.
🔧 Easy Solutions
To solve this issue, you need to ensure that the current view controller's view is in the window hierarchy before presenting the new view controller. Here are a few solutions:
Delay the presentation: This is a simple solution. Just delay the presentation of the new view controller until the current view controller's view is fully loaded and added to the window. You can do this by using a slight delay before presenting the new view controller, like so:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self presentViewController:finished animated:NO completion:NULL];
});
Wait for viewDidAppear: Since the error occurs when trying to present the view before it's part of the window hierarchy, you can wait for the current view controller's
viewDidAppear
method to be called before presenting the new view controller. Implement the following code:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIStoryboard *storyboard = self.storyboard;
finishViewController *finished = [storyboard instantiateViewControllerWithIdentifier:@"finishViewController"];
[self presentViewController:finished animated:NO completion:NULL];
}
Present from the parent view controller: If the view controller you want to present is contained within a parent view controller, you can try presenting the new view controller from the parent. This way, you ensure that the parent view controller's view is already in the window hierarchy.
UIStoryboard *storyboard = self.parentViewController.storyboard;
finishViewController *finished = [storyboard instantiateViewControllerWithIdentifier:@"finishViewController"];
[self.parentViewController presentViewController:finished animated:NO completion:NULL];
🌟 Call-to-Action
There you have it! Three easy solutions to present a UIViewController
on a UIViewController
not in the window hierarchy. Now, go ahead and give these solutions a try in your code!
Have you encountered this error before? How did you solve it? Let us know in the comments below. Happy coding! 😄
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.
