UIViewController
iOS
window hierarchy
iOS development
UIKit

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

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Attempting to present a UIViewController on another UIViewController whose view is not in the window hierarchy is a common mistake that iOS developers encounter. This article delves into technical explanations, examples, and solutions to better understand and solve this issue.

Understanding the Hierarchy

Every UIViewController in iOS has an associated view that is part of a view hierarchy. When presenting a new UIViewController, the presenting view controller's view must be in the current window's hierarchy to ensure a seamless and functional user interface. Presenting a view controller involves placing it onto the view hierarchy, making the new view controller the focus of user interaction.

The View Hierarchy

The view hierarchy is a tree-like structure composed of views that render content on the screen. At the top of this hierarchy is the window (UIWindow), which acts as a backdrop for all UIViewControllers. A typical structure can be depicted as follows:

 
1UIWindow
2└── UIViewController (Root)
3    ├── UIViewController (Child)
4    └── UIViewController (Presented)

When attempting to present a view controller, iOS expects the presenting view controller's view to be visible within this structure.

Common Causes of the "Not in the Window Hierarchy" Error

  1. View Controller is Not Visible: Attempting to present a view controller when the current one isn't displayed can result in this error.
  2. Incorrect View Transitions: Mismanaging view transitions or failing to add the presenting view controller's view to the window hierarchy can lead to issues.
  3. Asynchronous Operations: Starting asynchronous tasks that attempt to present view controllers upon completion without checking the view hierarchy state is a common pitfall.
swift
1if let currentVC = UIApplication.shared.windows.first?.rootViewController {
2    // This will fail if currentVC's view is not in the window's hierarchy
3    currentVC.present(newViewController, animated: true, completion: nil)
4}

Diagnosing the Problem

Symptom

This error often results in a runtime exception with a message similar to:

 
Attempt to present <NewViewController> on <CurrentViewController> whose view is not in the window hierarchy.

Debugging Steps

  • Check Hierarchy Presence: Verify that the view controller attempting to present another is indeed part of the window hierarchy.
  • Utilize Lifecycle Methods: Make sure presentations wait until the viewDidAppear phase of the lifecycle, as this guarantees the view is in the window hierarchy.

Solutions

Delayed Presentation

Oftentimes, ensuring the presentation code runs when the view is part of the hierarchy is enough. Using lifecycle methods is a standard approach:

swift
1override func viewDidAppear(_ animated: Bool) {
2    super.viewDidAppear(animated)
3    // Safe to present
4    self.present(newViewController, animated: true, completion: nil)
5}

Reattach Views

If you must present while the current view is detached from the hierarchy, consider reattaching:

swift
1func attachAndPresent() {
2    if let window = UIApplication.shared.windows.first {
3        window.rootViewController = self
4        self.present(newViewController, animated: true, completion: nil)
5    }
6}

Best Practices

  • Always ensure that your view controller's view is a part of the window hierarchy before presenting new view controllers.
  • Utilize Apple's lifecycle events to manage view controllers correctly.
  • Use completion handlers from asynchronous processes to verify view hierarchy state before attempting changes.

Summary Table

Issue/TechniqueDescription
Common ErrorAttempt to present on a view controller whose view is not in the hierarchy
Presentation FailureOccurs during a runtime if hierarchy checks are not in place
Lifecycle UseUse viewDidAppear to safely present new view controllers
Reattach StrategyTemporarily attach view controller views to the window if necessary
Best PracticeEnsure all view controllers use proper lifecycle awareness and asynchronous operation management

In conclusion, understanding and managing the view hierarchy is crucial to ensure smooth transitions and interactions in your app. By adhering to lifecycle methods and verifying hierarchy presence, developers can avoid errors relating to view presentations.


Course illustration
Course illustration

All Rights Reserved.