Programmatically add a Navigation Controller to your project

objective C demo
swift 3 demo

AppDelegate.swift

SWIFT 3

ViewController

CustomViewController.swift

OBJECTIVE C

The whole idea is that your window strongs UINavigationController, and UINavigationController strongs your main ViewController.

The UINavigationController is set up in your AppDelegate. Then your UINavigationController is init-ed with your main view controller.

nav-1

Then, in your main view controller, use self.navigationItem to set up the buttons and title.

nav-2

UINavigationController

First we create a UINavigationController property.

Then, we create the UINavigationController and init it with the main view controller of your project.
Set the UINavigationController as the root view controller of our window.

In your main view controller, make sure in your viewDidLoad, you set the navigation button like so:

The nav button has a button responder:

This means when you click on the nav button, we want to navigate to a new view controller. Hence, let’s implement the view controller that we want to navigate to.

File >> New >> File >> Cocoa Touch Class

Put in AddViewController for the “class name”.
Put in UIViewController for the “subclass of”.

Then import AddViewController to your main view controller, and implement the button responder like so:

Make sure you set background color for every view controller because if its default as clear, there may be animation issues.