Table of contents
1.
Introduction
2.
Using ViewController
3.
Properties of NavigationController
4.
Methods of NavigationController
5.
Example on Navigation Controller
6.
Frequently Asked Questions
6.1.
What is the UIApplicationDelegate?
6.2.
What is the UIResponder?
6.3.
What is the UIColorPickerViewController?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Navigation Controller in iOS

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

We spoke about segues for navigation between view controllers in this tutorial. On the other hand, the segue is not a good solution for traveling between two view controllers because it always keeps a new view controller while navigating backward instead of simply popping out that view controller.

In this segment of the course, we'll talk about navigating more effectively. The Navigation Controller is a container view controller for traversing hierarchical content that keeps a stack of View Controllers in place. It's a member of the UINavigationController class, which is descended from UIViewController.

Syntax:

class UInavigationController : UIViewController

Using ViewController

The Navigation Controller is in charge of one or more child view controllers in the navigation interface. Almost every iOS application makes use of navigation controllers. Whether one or more child view controllers are maintained in the navigation stack, only one view controller displays on the screen at a time. A new view controller appears on the screen when an item in the view controller is selected. Because this procedure is animated, the preceding view controller is hidden. Let's take a peek at the iOS settings app's navigation design.

A navigation bar with the view controller's title and a back button is present in all View Controllers contained in the Navigation Controller. By tapping the back button, the top view controller is removed from the navigation stack. The stack's root view, on the other hand, does not have a back button. The View Controllers are managed by a Navigation Controller in an ordered array, with the first item serving as the root view controller at the bottom of the navigation stack. The topmost view controller, which is now displayed, is the last item in the array. Using the methods of the UINavigationController class, we may push or pop View Controllers into the stack.

The image shown below shows the objects managed by the navigation controller.

Image Source

Explanation: The view on the topmost view controller in the navigation stack is adopted by a Navigation Controller. The view property of the navigation controller can be used to access this view. On the other hand, the view includes the navigation bar, content view, and optional toolbar. The navigation bar and toolbar content changes, but the views remain the same.

Properties of NavigationController

There are several properties that the UINavigationController class provides us to use the different features of this class.

Here we have discussed some of its important properties rest you can find them on the official site of developer.apple.

Methods of NavigationController

Apart from the properties, this class also provides us with a number of methods.

Example on Navigation Controller

Here we discuss an example, we create two screens one is the home screen, and another contains the color which we send through the home screen.

Class to make the navigations.

Code:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        ...
        let navigationController = UINavigationController(rootViewController: pickColorVC)


        window = UIWindow(frame: UIScreen.main.bounds)
        if let window = window {
          window.rootViewController = navigationController
          window.makeKeyAndVisible()
        }
        return true
    }
    ...
}

Explanation: In this example, we create a navigation controller and give it the color picker view controller as its root view controller. The navigation controller is also assigned as the window object's root view controller.

This class applies the navigation.

Code:

class ColorPickerViewController: UIViewController {


    @IBAction func didTapRedButton(sender: Any) {
        pushViewController(title: "Red", color: UIColor.red)
    }


    @IBAction func didTapBlueButton(sender: Any) {
        pushViewController(title: "Blue", color: UIColor.blue)
    }


    private func pushViewController(title: String, color: UIColor) {
        let vc = UIViewController()
        vc.view.backgroundColor = color
        vc.title = title
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

Explanation: When a button is pressed, we create a new view controller and put it onto the navigation stack using pushViewController.

Output:

Frequently Asked Questions

What is the UIApplicationDelegate?

UIApplicationDelegate is a protocol that your app must implement to be notified of user events like app launch, app switching to the background or foreground, app termination, or the opening of a push notification.

What is the UIResponder?

In a nutshell, UIResponder instances are objects that can handle and respond to various events. UIResponders are used by UIView, UIViewController, UIWindow, UIApplication, and UIApplicationDelegate in iOS.  

What is the UIColorPickerViewController?

To give your app additional control over the process, the UIColorPickerViewController logs user involvement using a UIColorWell. For example, you can alter supportsAlpha to false to hide the alpha slider and only allow opaque colors by setting the initial selectedColor.

Conclusion

In this article, we have extensively discussed the Navigation Controller in iOS. We discussed how this navigation works and also one simple example to clarify the concept.

We hope that this blog has helped you enhance your knowledge regarding the Navigation Controller. Do check out the awesome content on the Coding Ninjas Website, Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass