6 June 2024 Leave a comment Tech-Help
When developing iOS applications, you might encounter the following warning in the console:
Warning: Attempt to present < finishViewController: 0x1e56e0a0 > on < ViewController: 0x1ec3e000 > whose view is not in the window hierarchy!
This warning typically occurs when trying to present a view controller before the current view controller’s view is fully loaded into the window hierarchy. Below are steps to troubleshoot and resolve this issue.
Common Causes and Solutions
1. Presenting in viewDidLoad
Attempting to present a view controller in the viewDidLoad
method can lead to this warning. Instead, move the presentation code to the viewDidAppear:
method:
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self presentViewController:finished animated:NO completion:NULL]; }
However, be cautious as presenting a view controller in viewDidAppear:
may cause it to be presented every time the view appears.
2. Multiple Segue Triggers
This error can also occur if you accidentally trigger multiple segues for the same action. Ensure that only one segue is being called:
// Ensure that only one segue is being called if (![self isBeingPresented]) { [self performSegueWithIdentifier:@"yourSegueIdentifier" sender:self]; }
3. Using viewWillLayoutSubviews or viewDidLayoutSubviews
Another approach is to use viewWillLayoutSubviews
or viewDidLayoutSubviews
for presenting the view controller, as these methods are called earlier than viewDidAppear:
:
- (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; if (!self.isViewLoaded) { [self presentViewController:finished animated:NO completion:NULL]; } }
4. Ensuring the Correct Root View Controller
Ensure that you are presenting the view controller from the correct root view controller:
UIViewController *currentViewController = [UIApplication sharedApplication].keyWindow.rootViewController; while (currentViewController.presentedViewController) { currentViewController = currentViewController.presentedViewController; } [currentViewController presentViewController:yourViewController animated:YES completion:nil];
Advanced Techniques
In some cases, using addChildViewController:
can help resolve the issue:
- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:navigationViewController.view]; [self addChildViewController:navigationViewController]; }
Conclusion
By following the above steps, you can effectively troubleshoot and resolve the “Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy” error. Ensuring that view controllers are presented at the right time and from the correct context is crucial for a smooth user experience.
Streamlining Automated Testing with Repeato
For mobile developers, ensuring that your app behaves as expected across different scenarios can be time-consuming. This is where Repeato, a No-code test automation tool for iOS and Android, can be incredibly helpful. With Repeato, you can create, run, and maintain automated tests quickly and efficiently, allowing you to focus on developing a great product. Repeato’s computer vision and AI capabilities make it easy to forward test automation tasks to non-technical colleagues or QAs, streamlining your development process.
For more information, you can refer to our documentation on getting started with Repeato.