Hierarchical Navigation

View Controllers

You have probably noticed that so far, all of our “logic” code was written in a subclass of UIViewController. As the name suggests, this object manages our UIView (which is made up of subviews and actions and outlets).

As your applications grow, the UI is not all going to fit on screen. It will have to be split into different views, which in turn will each need their own view controller.

Navigation Controllers

There are many different ways to navigate between view controllers. It can be done manually, or by using a “view controller controller”. When working with hierarchical views, a good option is to use a UINavigationController.

The UINavigationController holds view controllers in a collection called a navigation stack. The first displayed view controller, called the root view controller, is at the bottom of the stack. The currently displayed view controller is at the top of the stack. You push new view controllers on the stack for display. You pop view controllers from the stack to display what’s below them.

You can access the navigation controller from any UIViewController in the stack using its navigationController property.

The UINavigationController can optionally display a navigation bar at the top and a navigation toolbar at the bottom.

The navigation bar is updated automatically by the controller:

  • It sets a label in the center, which identifies the current view controller by its title property.
  • When the current view is not the root view controller, it adds a back button on the left to pop the view, identified by the title of the previous view controller.
These items can be customized at the view controller level by accessing its navigationItem property, and by editing that UINavigationItem‘s barBackButtonItem and title properties respectively.

You can also add a custom button on the right by setting the rightBarButtonItem property on the navigationItem.

The navigation toolbar is used for optional controls, which are set using the toolbarItems property of the visible view controller. The toolbar is hidden by default, and can be displayed by calling setToolbarHidden:animated: on the navigation controller.

Segues

A segue is the transition between two view controllers. A UIStoryboardSegue object contains information about the view controllers involved in a transition.

You never create segue objects directly, but do so in the Storyboard. This is done by control-dragging from an actionable object in one view controller, like a button, to another view controller. There are a few different types of segues, and we’ll look at them later in the course, but we should always use push for hierarchical navigation.

A segue can be identified by filling in its “Identifier” property. This becomes useful in code, when manually triggering a segue, and when preparing for a segue to occur.

You can manually trigger a segue from a view controller using the performSegueWithIdentifier:sender: method.

You can prepare the next view controller that will appear by implementing the prepareForSegue:sender: method in the current view controller. This is useful if you have a modular view controller that you need to customize before displaying.


  1. View Controller Programming Guide for iOS. Apple, Visited Sep 24 2012.

Comments are closed.