About View Controllers
View controllers are a vital link between an app’s data and its visual appearance. Whenever an iOS app displays a user interface, the displayed content is managed by a view controller or a group of view controllers coordinating with each other. Therefore, view controllers provide the skeletal framework on which you build your apps.
iOS provides many built-in view controller classes to support standard user interface pieces, such as navigation and tab bars. As part of developing an app, you also implement one or more custom controllers to display the content specific to your app.
At a Glance
View controllers are traditional controller objects in the Model-View-Controller (MVC) design pattern, but they also do much more. View controllers provide many behaviors common to all iOS apps. Often, these behaviors are built into the base class. For some behaviors, the base class provides part of the solution and your view controller subclass implements custom code to provide the rest. For example, when the user rotates the device, the standard implementation attempts to rotate the user interface; however, your subclass decides whether the user interface should be rotated, and, if so, how the configuration of its views should change in the new orientation. Thus, the combination of a structured base class and specific subclassing hooks make it easy for you to customize your app’s behavior while conforming to the platform design guidelines.
A View Controller Manages a Set of Views
A view controller manages a discrete portion of your app’s user interface. Upon request, it provides a view that can be displayed or interacted with. Often, this view is the root view for a more complex hierarchy of views—buttons, switches, and other user interface elements with existing implementations in iOS. The view controller acts as the central coordinating agent for this view hierarchy, handling exchanges between the views and any relevant controller or data objects.
You Manage Your Content Using Content View Controllers
To present content that is specific to your app, you implement your own content view controllers. You create new view controller classes by subclassing either the
UIViewController class or the
UITableViewController class, implementing the methods necessary to present and control your content.
Container View Controllers Manage Other View Controllers
Container view controllers display content owned by other view controllers. These other view controllers are explicitly associated with the container, forming a parent-child relationship. The combination of container and content view controllers creates a hierarchy of view controller objects with a single root view controller.
Each type of container defines its own interface to manage its children. The container’s methods sometimes define specific navigational relationships between the children. A container can also set specific restrictions on the types of view controllers that can be its children. It may also expect the view controllers that are its children to provide additional content used to configure the container.
iOS provides many built-in container view controller types you can use to organize your user interface.
Presenting a View Controller Temporarily Brings Its View Onscreen
Sometimes a view controller wants to display additional information to the user. Or perhaps it wants the user to provide additional information or perform a task. Screen space is limited on iOS devices; the device might not have enough room to display all the user interface elements at the same time. Instead, an iOS app temporarily displays another view for the user to interact with. The view is displayed only long enough for the user to finish the requested action.
To simplify the effort required to implement such interfaces, iOS allows a view controller to present another view controller’s contents. When presented, the new view controller’s views are displayed on a portion of the screen—often the entire screen. Later, when the user completes the task, the presented view controller tells the view controller that presented it that the task is complete. The presenting view controller then dismisses the view controller it presented, restoring the screen to its original state.
Presentation behavior must be included in a view controller’s design in order for it to be presented by another view controller.
Storyboards Link User Interface Elements into an App Interface
A user interface design can be very complex. Each view controller references multiple views, gesture recognizers, and other user interface objects. In return, these objects maintain references to the view controller or execute specific pieces of code in response to actions the user takes. And view controllers rarely act in isolation. The collaboration between multiple view controllers also defines other relationships in your app. In short, creating a user interface means instantiating and configuring many objects and establishing the relationships between them, which can be time consuming and error prone.
Instead, use Interface Builder to create storyboards. A storyboard holds preconfigured instances of view controllers and their associated objects. Each object’s attributes can be configured in Interface Builder, as can relationships between them.
At runtime, your app loads storyboards and uses them to drive your app’s interface. When objects are loaded from the storyboard, they are restored to the state you configured in the storyboard. UIKit also provides methods you can override to customize behaviors that cannot be configured directly in Interface Builder.
By using storyboards, you can easily see how the objects in your app’s user interface fit together. You also write less code to create and configure your app’s user-interface objects.
How to Use This Document
Start by reading “View Controller Basics,” which explains how view controllers work to create your app’s interface. Next, read “Using View Controllers in Your App” to understand how to use view controllers, both those built into iOS and those you create yourself.
When you are ready to implement your app’s custom controllers, read “Creating Custom Content View Controllers” for an overview of the tasks your view controller performs, and then read the remaining chapters in this document to learn how to implement those behaviors.
Before reading this document, you should be familiar with the content in Start Developing iOS Apps Today and Your Second iOS App: Storyboards. The storyboard tutorial demonstrates many of the techniques described in this book, including the following Cocoa concepts:
Defining new Objective-C classes
The role of delegate objects in managing app behaviors
The Model-View-Controller paradigm
For more information about the standard container view controllers provided by UIKit, see View Controller Catalog for iOS.
For guidance on how to manipulate views in your view controller, see View Programming Guide for iOS.
For guidance on how to handle events in your view controller, see Event Handling Guide for iOS.
For more information about the overall structure of an iOS app, see iOS App Programming Guide.
For guidance on how to configure storyboards in your project, see Xcode Overview