- Developing with Navigation Controllers and Split Views
- Recipe: Building a Simple Two-Item Menu
- Recipe: Adding a Segmented Control
- Recipe: Navigating Between View Controllers
- Recipe: Presenting a Custom Modal Information View
- Recipe: Page View Controllers
- Recipe: Scrubbing Pages in a Page View Controller
- Recipe: Tab Bars
- Recipe: Remembering Tab State
- Recipe: Building Split View Controllers
- Recipe: Creating Universal Split View - Navigation Apps
- Recipe: Custom Containers and Segues
- One More Thing: Interface Builder and Tab Bar Controllers
View controllers simplify view management for many iOS applications. They allow you to build applications that centralize many tasks including view management, orientation changes, and view unloading during low memory conditions. Each view controller owns a hierarchy of views, which presents a complete element of a unified interface.
In the previous chapter, you built view controller-based applications using Xcode and Interface Builder. Now it's time to take a deeper look at using view controller-based classes and how to apply them to real-world situations for both iPhone/iPod and iPad design scenarios. In this chapter you discover how to build simple menus, create view navigation trees, design tab bar- and page view-based applications, and more. This chapter offers hands-on recipes for working with a variety of controller classes.
Developing with Navigation Controllers and Split Views
The UINavigationController class offers one of the most important ways of managing interfaces on a device with limited screen space such as the iPhone and iPod touch. They create a way for users to drill up and down a hierarchy of interface presentations, to create a virtual GUI that's far larger than the device. Navigation controllers fold their GUIs into a neat tree-based scheme. Users travel through that scheme using buttons and choices that transport them around the tree. You see navigation controllers in the Contacts application and in Settings, where selections lead to new screens and back-buttons move to previous ones.
Several standard GUI elements identify the use of navigation controllers in applications, as seen in Figure 5-1 (left). These include their large navigation bars that appear at the top of each screen, the backward pointing button at the top-left that appears when drilling into hierarchies, and option-buttons at the top-right that offer other application functionality like editing. Many navigation controller applications are built around scrolling lists, where elements in that list lead to new screens, indicated by grey and blue chevrons found on the right side of each table cell.
The iPad, with its large screen size, doesn't require the kind of space saving shortcuts that navigation controllers leverage on the iPhone and iPod touch along with their cousins the tab view controller and modal view controller. iPad applications can use navigation controllers directly but the UISplitviewController shown in Figure 5-1 (right) offers a presentation that's far better suited for the more expansive device.
Notice the differences between the iPhone implementation on the left and the iPad implementation on the right of Figure 5-1. The iPad's split view controller contains no chevrons. When items are tapped, their data appears on the same screen using the large right-hand detail area. The iPhone, lacking this space, presents chevrons that indicate new views will be pushed onscreen. Each approach takes device-specific design into account in its presentation.
Figure 1 (Click to Enlarge) The iPhone's Navigation Controller uses chevrons to indicate that detail views will be pushed onscreen when their parents are selected. On the iPad, Split View Controllers use the entire screen, separating navigation elements from detail presentations.
Both the iPhone and iPad Inbox views use similar navigation controller elements including the back button (iPad Book/Gmail for Book), an options button (Edit), and a status in the title bar (with its (1) unread message). Each of these elements is created using navigation controller API calls working with a hierarchy of e-mail accounts and mailboxes. The difference lies at the bottom of the navigation tree, at the level of individual messages that form the leaves of the data structure. On the iPhone, leaves are indicated by chevrons and, when viewed, are pushed onto the navigation stack, which accumulates the trace of a user's progress through the interface. On the iPad, leaves are presented in a separate view without those chevrons that otherwise indicate that users have reached the extent of the hierarchy traversal.
iPhone-style navigation controllers play roles as well on the iPad. When iPad applications use standard (iPhone-style) navigation controllers, they usually do so in narrow contexts such as transient popover presentations, where the controller is presented on-screen in a small view with a limited lifetime. Otherwise, iPad applications are encouraged to use the split view approach that occupies the entire screen.
Using Navigation Controllers and Stacks
Every navigation controller owns a root view controller. This controller forms the base of its stack. You can programmatically push other controllers onto the stack as the user makes choices while navigating through the model's tree. Although the tree itself may be multidimensional, the user's path, essentially his history, is always a straight line representing the choices already made to date. Moving to a new choice extends the navigation breadcrumb trail and automatically builds a Back button each time a new view controller gets pushed onto the stack.
Users can tap a back button to pop controllers off the stack. The name of each button represents the title of the most recent view controller. As you return through the stack of previous view controllers, each back button previews the view controller that can be returned to. Users can pop back until reaching the root. Then you can go no further. The root is the root, and you cannot pop beyond that root.
This stack-based design lingers even when you plan to use just one view controller. You might want to leverage the UINavigationController’s built-in navigation bar to build a simple utility that uses two-button menu, for example. This would disregard any navigational advantage of the stack. You still need to set that one controller as the root via initWithRootViewController:. Storyboards simplify using navigation controllers for one- and two-button utilities, as you read about in Chapter 4.
Pushing and Popping View Controllers
Add new items onto the navigation stack by pushing a new controller with pushViewController:animated:. Send this call to the navigation controller that owns a UIViewController. This is normally called on self.navigationController when working with a primary view controller class. When pushed, the new controller slides onscreen from the right (assuming you set animated to YES). A left-pointing Back button appears, leading you one step back on the stack. The Back button uses the title of the previous view controller.
There are many reasons you’d push a new view. Typically, these involve navigating to subviews like detail views or drilling down a file structure or preferences hierarchy. You can push controllers onto the navigation controller stack after your user taps a button, a table item, or a disclosure accessory.
There’s little reason to ever subclass UINavigationController. Perform push requests and navigation bar customization (like setting up a bar’s right-hand button) inside UIViewController subclasses. For the most part, you don't access the navigation controller directly. The two exceptions to this rule include managing the navigation bar’s buttons and when you change the bar’s look.
You might change a bar style or its tint color by accessing the navigationBar property directly.
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
To add a new button you modify your navigationItem, which provides an abstract class that describes the content shown on the navigation bar including its left and right bar button item and its title view. Here's how you can assign a button to the bar. To remove a button, assign the item to nil.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Action" style:UIBarButtonItemStylePlain target:self action:@selector(performAction:)] autorelease];
Bar button items are not views. They are abstract classes that contain titles, styles, and callback information that are used by navigation items and toolbars to build actual buttons into interfaces. iOS does not provide you with access to the button views built by bar button items and their navigation items.
The Navigation Item Class
The objects that populate the navigation bar are put into place using the UINavigationItem class, which is an abstract class that stores information about those objects. Navigation item properties include the left and right bar button items, the title shown on the bar, the view used to show the title, and any Back button used to navigate back from the current view.
This class enables you to attach buttons, text, and other UI objects into three key locations: the left, the center, and the right of the navigation bar. Typically, this works out to be a regular button on the right, some text (usually the UIViewController’s title) in the middle, and a Back-styled button on the left. But you’re not limited to that layout. You can add custom controls to any of these three locations You can build navigation bars with search fields, segment controls, toolbars, pictures, and more.
You’ve already seen how to add custom bar button items to the left and right of a navigation item. Adding a custom view to the title is just as simple. Instead of adding a control, assign a view. This code adds a custom UILabel, but this could be a UIImageView, a UIStepper, or anything else.
self.navigationItem.titleView = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f,0.0f, 120.0f, 36.0f)] autorelease];
The simplest way to customize the actual title is to use the title property of the child view controller rather than the navigation item.
self.title = @"Hello";
When you want the title to automatically reflect the name of the running application, here is a little trick you can use. This returns the short display name defined in the bundle’s Info.plist file. Limit using application-specific titles (rather than view-related titles) to simple utility applications.
self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
With normal navigation controllers, you push your way along views, stopping occasionally to pop back to previous views. That approach assumes that you’re drilling your way up and down a set of data that matches the tree-based view structure you’re using. Modal presentation offers another way to show a view controller. After sending the presentModalViewController:animated: message to a navigation controller, a new view controller slides up into the screen and takes control until it’s dismissed with dismissModalViewController:Animated:. This enables you to add special-purpose dialogs into your applications that go beyond alert views.
Typically, modal controllers are used to pick data such as contacts from the Address Book or photos from the Library or to perform a short-lived task like sending e-mail or setting preferences. Use modal controllers in any setting where it makes sense to perform a limited-time task that lies outside the normal scope of the active view controller.
You can present a modal dialog in any of four ways, controlled by the modalTransitionStyle property of the presented view controller. The standard, UIModalTransitionStyleCoverVertical, slides the modal view up and over the current view controller. When dismissed it slides back down. UIModalTransitionStyleFlipHorizontal performs a back-to-front flip from right-to-left. It looks as if you’re revealing the back side of the currently presented view. When dismissed, it flips back left-to-right. UIModalTransitionStyleCrossDissolve fades the new view in over the previous one. On dismissal, it fades back to the original view. Use UIModalTransitionStylePartialCurl to curl up content (in the way the Maps application does) to reveal a modal settings view "underneath" the primary view controller.
On the iPhone and iPod touch, modal controllers always fully take over the screen. The iPad offers more nuanced presentations. You can introduce modal items using three presentation styles. In addition to the default fullscreen style (UIModalPresentationFullScreen), use UIModalPresentationFormSheet to present a small overlay in the center of the screen or UIModalPresentationPageSheet to slide up a sheet in the middle of the screen. These styles are best experienced in landscape mode to visually differentiate the page sheet presentation from the full screen one.