About View Controllers

For iOS applications, view controllers provide a vital link between an application’s data and its visual appearance. Understanding when and how to use view controllers is crucial to the design of iOS applications. View controllers are traditional controller objects in the Model-View-Controller design paradigm but they also do much more. In iOS applications, view controllers provide much of the logic needed to manage basic application behaviors. For example, view controllers manage the presentation and removal of content from the screen and they manage the reorientation of views in response to device orientation changes.

At a Glance

View controllers exist to make it easier for you to create applications that conform to the platform design guidelines. However, the default view controller classes can only do so much. At some point, your custom code must take over and finish the job. This document shows you the basic behaviors that all view controllers provide and where you can customize that behavior to suit your own needs.

View Controllers Manage a View Hierarchy

Each view controller is responsible for managing a discrete part of your application’s user interface. View controllers are directly associated with a single view object but that object is often just the root view of a much larger view hierarchy that is also managed by the view controller. The view controller acts as the central coordinating agent for the view hierarchy, handling exchanges between its views and any relevant controller or data objects. A single view controller typically manages the views associated with a single screen’s worth of content, although in iPad applications this may not always be the case.

You Manage Your Content Using a Custom View Controller

Whenever you want to present content that is specific to your application, you do so using a custom view controller. You create custom view controllers by subclassing either UIViewController or UITableViewController directly and implementing the methods necessary to present your content. At a minimum, a custom view controller must be capable of presenting and managing the views associated with your custom content. You may also need to implement other view controller methods to customize behaviors such as rotation, memory management, event handling, and to interact with other view controllers in your application.

Navigation Controllers Manage Stacks of Other View Controllers

A navigation controller is an instance of the UINavigationController class that you use as-is in your application. Applications that contain structured content can use navigation controllers to navigate between different levels of content. The navigation controller itself manages the display of one or more custom view controllers, each of which manages the data at a specific level in your data hierarchy. The navigation controller also provides controls for determining the current location in this data hierarchy and for navigating back up the hierarchy.

Tab Bar Controllers Manage Independent Sets of View Controllers

A tab bar controller is an instance of the UITabBarController class that you use as-is in your application. Applications use tab bar controllers to manage multiple distinct interfaces, each of which consists of any number of custom views and view controllers. The tab bar controller also manages interactions with a tab bar view, which the user taps to change the currently selected interface. For example, the iPod application on iPhone and iPod touch uses a tab bar interface where each tab represents a different way of viewing the user’s music and media.

iPad Supports Special Containers for Your Content

The larger screen size of iPad provides new opportunities for presenting content. The UISplitViewController class manages the implementation of a master-detail interface, where both the master and detail portions of the interface are themselves managed by view controllers. Although the UIPopoverController class is not a view controller itself, it works with your application’s view controllers to present content in a floating view.

Modal View Controllers Temporarily Interrupt the Current Workflow

Any view controller can be presented modally. The purpose of modal view controllers is to interrupt the current workflow temporarily to gather or present information. For example, you might use modal view controllers to display preferences or configuration options, gather data from the user, or even facilitiate transitions between distinct portrait and landscape representations of the current screen. Because they always involve a full-screen transition, the use of modally presented view controllers is used only sparingly in iPad applications but is fairly common in iPhone applications.

Compose view controllers are a specific type of view controller that is almost always presented modally. Compose view controllers are defined by the system and used to present specific interfaces, such as for composing email or SMS messages. On iPhone and iPod touch, you always present compose controllers modally but on iPad you may also present them using a popover.

View Controllers Can Be Combined to Create Sophisticated Layouts

In all but the simplest applications, it is common to find multiple view controllers working together. Navigation, tab bar, and split view controllers always work in conjunction with other view controllers, and even your custom view controllers may occasionally need to present other view controllers modally. However, some combinations of view controllers work better than others. Combining view controllers in ways that make sense is important to creating a straightforward, easily navigable user interface.

Prerequisites

Before you start reading this document, you should have at least a fundamental understanding of the following Cocoa concepts:

Developers who are new to Cocoa and Objective-C can get information about all of these topics in Cocoa Fundamentals Guide.

Development of iOS applications requires an Intel-based Macintosh computer running Mac OS X v10.5 or later. You must also download and install the iOS SDK. For information about how to get the iOS SDK, go to the Apple Developer website.

See Also

For additional information about application design, see App Programming Guide for iOS.

For guidance about how to design iOS applications, see iOS Human Interface Guidelines.

For information about the view controller classes discussed in this document, see UIKit Framework Reference.