Navigation controller is one of the most common parts of iPhone applications. As the name suggests, navigation controller controls the navigation with in an application and provides a simple and easy access to other views of the same application. UINavigationController is the main class that handles the functionality of navigation controller. UINavigationController is a subclass of UIViewController. Navigation controller itself is rarely customized beyond the visuals of the navigation bar. An instance of UINavigationController can be created either in code or in an XIB file. It is thought of as a stack. It has a root view controller, and then new view controllers can be pushed onto the stack or popped off the stack (often by pressing the back button).

Modifying the Navigation Stack

Push to add a view controller

- (void)pushViewController:(UIViewController *)viewController

Pop to remove a view controller

- (UIViewController *)popViewControllerAnimated:(BOOL)animated;

Set to change the entire stack of view controllers
(iPhone OS 3.0)

- (void)setViewControllers:(NSArray *)viewControllers

Pushing Your First View Controller

- (void)applicationDidFinishLaunching
// Create a navigation controller
navController = [[UINavigationController alloc] init];
// Push the first view controller on the stack
[navController pushViewController:firstViewController
// Add the navigation controller’s view to the window
[window addSubview:navController.view];

Push from within a view controller on the stack.

- (void)someAction:(id)sender
// Potentially create another view controller
UIViewController *viewController = ...;
[self.navigationController pushViewController:viewController

Almost never call pop directly! Automatically invoked by the back button.

Connecting View Controllers

Multiple view controllers may need to share data. One may need to know about what another is doing

  • Watch for added, removed or edited data
  • Other interesting events

Don’t do to Share Data

  • Global variables or singletons. This includes your application delegate!
  • Direct dependencies make your code less reusable and more difficult to debug & test

Best Practice for Data Flow

Figure out exactly what needs to be communicated. Define input parameters for your view controller. For communicating back up the hierarchy, use loose coupling. Define a generic interface for observers (like delegation)


Describes appearance of the navigation bar

  • Title string or custom title view
  • Left & right bar buttons
  • More properties defined in UINavigationBar.h

Every view controller has a navigation item for customizing that is displayed when view controller is on top of the stack.

Displaying a Title

UIViewController already has a title property

  • @property(nonatomic,copy) NSString *title;

Navigation item inherits automatically. Previous view controller’s title is displayed in back button

viewController.title = @“Detail”;

Left & Right Buttons

UIBarButtonItem is a special object, defines appearance & behavior for items in navigation bars and toolbars. Display a string, image or predefined system item like Target + action (like a regular button)

Text Bar Button Item

- (void)viewDidLoad
UIBarButtonItem *fooButton = [[UIBarButtonItem alloc]
self.navigationItem.leftBarButtonItem = fooButton;
[fooButton release];

System Bar Button Item

- (void)viewDidLoad
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];

Edit/Done Button

Very common pattern and every view controller has one available. Target/action already set up.

self.navigationItem.leftBarButtonItem = self.editButtonItem;
// Called when the user toggles the edit/done button
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
// Update appearance of views

Custom Title View

Arbitrary view in place of the title


UISegmentedControl *segmentedControl = ...
self.navigationItem.titleView = segmentedControl;
[segmentedControl release];

Back Button

Sometimes a shorter back button is needed

self.title = @“Hello there, CS193P!”;
UIBarButtonItem *heyButton = [[UIBarButtonItem alloc]
self.navigationItem.backButtonItem = heyButton;
[heyButton release];



Tagged with: C/C++ languageiPhone

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.