iOS

UINavigationController

Remarks#

From the documentation:

The UINavigationController class implements a specialized view controller that manages the navigation of hierarchical content. This navigation interface makes it possible to present your data efficiently and makes it easier for the user to navigate that content. You generally use this class as-is but you may also subclass to customize the class behavior.

Popping in a Navigation Controller

To previous view controller

To pop back to the previous page you can do this:

Swift

navigationController?.popViewControllerAnimated(true)

Objective-C

[self.navigationController popViewControllerAnimated:YES];

To root view controller

To pop to the root of the navigation stack, you can do this:

Swift

navigationController?.popToRootViewControllerAnimated(true)

Objective C

[self.navigationController popToRootViewControllerAnimated:YES];

Creating a NavigationController

In your storyboard select the ViewController that you want to embed into a Navigation Controller.

Then navigate to Editor > Embed In > Navigation Controller

enter image description here

And that will create your navigation controller

enter image description here

Embed a view controller in a navigation controller programmatically

Swift

//Swift
let viewController = UIViewController()
let navigationController = UINavigationController(rootViewController: viewController)

//Objective-C
UIViewController *viewController = [[UIViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

Pushing a view controller onto the navigation stack

//Swift
let fooViewController = UIViewController()
navigationController?.pushViewController(fooViewController, animated: true)

//Objective-C
UIViewController *fooViewController = [[UIViewController alloc] init];
[navigationController pushViewController:fooViewController animated:YES];

Purpose

UINavigationController is used to form a tree-like hierarchy of view controllers, which is known as a navigation stack.

From developers perspective:

You can connect independently made controller and get all the benefits of a free hierarchy manager and common UI presenter gratis. UINavigationController animates the transition to new controllers and provides the back functionality for you automatically. UINavigationControlleralso gives access to all the other controllers in the navigation stack which can help access to some functionality or data.

From user’s perspective:

UINavigationController helps to remember where user is at the moment (navigation bar title) and how he can go back (embedded back button) to one of the previous screens.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow