Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How to get the root view controller of a navigation controller?


Asked by Robin Gates on Dec 08, 2021 FAQ



You can access the top view controller through the topViewController computed property, but the UINavigationController class doesn't offer an API that easily lets you access the root controller. But it's easy to do. The root view controller is simply the view controller that sits at the bottom of the navigation stack.
Accordingly,
You can access the navigation controller's array of view controllers through its viewControllers property. To access the root view controller, we ask for the first item of the array of view controllers. Each view controller in the array is a child view controller and the navigation controller is the parent view controller.
Next, A navigation controller is a container view controller—that is, it embeds the content of other view controllers inside of itself. You access a navigation controller’s view from its view property.
In this manner,
In AppDelegate, the navigation controller comes from the window’s root view controller. In prepareForSegue it comes from the segue’s destination view controller. After that, though, the process for grabbing the first child of the navigation controller is the same.
Also Know,
A navigation controller object manages its child view controllers using an ordered array, known as the navigation stack. The first view controller in the array is the root view controller and represents the bottom of the stack.