In this tutorial, we have discussed segues for the navigation between view controllers. However, to navigate between two view controllers, the segue is not a good option as it always maintains a new view controller while navigating backward instead of just popping out that view controller.
In this section of the tutorial, we will discuss the better approach for navigation. The Navigation Controller can be defined as the container view controller that maintains a stack of View Controllers for navigating hierarchical content. It is an instance of the UINavigationController class, which inherits UIViewController.
The Navigation Controller manages one or more child view controllers in the navigation interface. Navigation Controllers are being used in almost every iOS application. Even though one or more child view controllers are managed into navigation stack, only one view controller appears on-screen at an instance. Selecting an item in the view controller pushes a new view controller on the screen. This process is animated and therefore hides the previous view controller. Let's look at the navigation interface used in the settings app on iOS.
All the View Controllers embedded in the Navigation Controller contain a navigation bar that contains the title of the view controller and back button. The top view controller is removed from the navigation stack by tapping the back button. However, the back button is not provided for the root view of the stack.
A Navigation Controller manages the View Controllers in the ordered array where the first item is considered as the root view controller and also the bottom of the navigation stack. The last item in the array is the topmost view controller, which is currently being displayed. We can push or pop View Controllers into the stack using the methods of UINavigationController class.
The navigation controller also manages the navigation bar for all the view controllers of the navigation stack, which always appears on the top of the screen. The navigation controller also manages an optional toolbar on the bottom of the screen.
A delegate object maintains the behavior of a navigation controller. It can provide the custom animations to pushing and popping the view controllers. It can also specify the preferred orientation for the navigation interface.
The Following image shows the objects managed by a navigation controller.
A Navigation Controller adopts the view of the topmost view controller in the navigation stack. We can access this view by using the view property of the navigation controller. However, the view contains the navigation bar, the content view, and the optional toolbar. The content of the navigation bar and the toolbar changes, whereas the views do not change.
Navigation Controller properties
Navigation Controller Methods
In this example, we will see how to embed the view controller into a navigation controller and how can we push a view controller onto the navigation stack.
We will get an initial view controller shown in the following image on creating a new project.
To embed this view controller in the navigation stack, go to Editor -> Embed in and choose Navigation Controller as shown in the following image.
This will create a navigation controller having a navigation bar at the top of it. Here, we must notice that the navigation controller is the initial view controller.
Here we define the navigation bar title for the view controller, and Let's add another view controller in the application so that we can push that view controller in the navigation stack. Define a storyboard id as SecondVC for this ViewController.
Here, we have added a button (show) by tapping on which the Second View Controller is pushed.