Navigation Controller iOS Tutorial

A navigation controller is responsible for managing the navigation of hierarchical content. The navigation controller manages the current displayed screens using the navigation stack. At the bottom of this stack is the root view controller and at the top is the view controller currently displayed. You use methods to push and pop view controllers on and of the stack. This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

For product name, use IOSNavigationControllerTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

Go to the Storyboard and delete the View Controller. Open the Object Library and drag a Navigation Controller to the Storyboard. Select the Navigation Controller and go to The Attribute inspector. In the View Controller section check the "Is Initial View Controller" checkbox.

The Navigation Controller will automatically contain a Table View Controller, delete this and drag a View Controller to the storyboard. Ctrl + drag from the Navigation Controller to the View Controller and select the “root view controller” segue.

Select the View Controller’s view and Go to the Attributes inspector. Change the background to light grey. Select the View Controller. Go to the Identity Inspector and change the class to “ViewController”.

Drag a Button from the Object Library and place it on top of the View Controller. Change the title to “Show First View Controller”.

Next, drag another View Controller to the Storyboard and place it next to the root view controller. Select the View Controller’s view and change the background to red in the Attributes inspector.

Ctrl + drag from the Root View Controller to the other View Controller and select the show segue

Select the Segue between the two View Controller and go to the Attributes inspector. Change the Identifier to SegueToFirstVC.

Add a new file to the project, select iOS -> Source -> Cocoa Touch Class. Name it FirstViewController and make it a subclass of UIViewController.

Select the right View Controller. Go to the Identity Inspector and change the class to “FirstViewController”.

The Storyboard should look like this.

Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the Button to the ViewController class and create the following Action

Go to the ViewController.swift file and change the viewDidLoad method to

override func viewDidLoad() {
  super.viewDidLoad()

  navigationItem.title = "Root View"
}

implement the showFirstViewController(_:) method

@IBAction func showFirstViewController(_ sender: Any) {
  performSegue(withIdentifier: "SegueToFirstVC", sender: self)
}

This will perform a segue from the root view controller to the first view controller.
Build and Run the project to see the navigation controller in action

The source code of the IOSNavigationControllerTutorial can be downloaded at the ioscreator repository on Github.