IOS Add Navigation Controller

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

When your application has multiple View Controllers, then you will also need to handle the navigation between them. If you at our simple demo application in which we have two view controllers and a button that performs a segue from the first to the second.

image

When you run this application, you will notice that the second screen is going from the bottom and there is no way to transition back to the previous screen.

image

To add the navigation controller open the storyboard file.

image

In the Document Outline, select the first View Controller, go to the Editor menu, and select Embed In > Navigation Controller option.

image

You will now see a third scene, which is a Navigation Controller scene, and Xcode has automatically changed it to the initial view controller.

image

Let's run your application, and you will see the navigation area at the top.

image

Now when you click on the button, you will get the animation coming in from the right-hand side, and you will also see the Back button on the second screen which will move back to the first screen.

image

On both screens, the navigation area at the top is blank, so let's add a title on both screens. Open the first view controller class and update the viewDidLoad method by setting the title property of view controller.

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        self.title = "First Scene"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
} 

This method is called automatically when the view controller is loaded. Similarly set the title property of the 2nd view controller.

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.title = "Second Scene"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
}

Let's run your application, and you will now see the title on both scenes.

image

Got any IOS Question?