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

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for 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:

  1. 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];
});
  1. 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];
}
  1. 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.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my