Chapter 8. Tab Bar and Navigation Applications

WHAT YOU WILL LEARN IN THIS CHAPTER

  • How to create Tab Bar applications

  • How to display Tab Bar applications in different orientations

  • How to create navigation-based applications

  • How to navigate from one View window to another in a navigation-based application

The previous chapter demonstrated how to create a multiview application by using multiple view controllers. In fact, multiview applications are so common that the iPhone has a special category of application named after it: Tab Bar applications. A Tab Bar application consists of a Tab Bar commonly located at the bottom of the screen. Within the Tab Bar are Tab Bar items, which when touched display a particular view. One good example of a Tab Bar application is the Phone application in iPhone (see Figure 8-1). The Phone application contains five Tab Bar items: Favorites, Recents, Contacts, Keypad, and Voicemail. For example, when you touch the Favorites item, you see a list of editable favorite contacts. Touching the Contacts item displays the entire list of contacts stored on your iPhone.

Figure 8-1

Figure 8.1. Figure 8-1

In addition to Tab Bar applications, a type of application commonly found on the iPhone is the navigation-based application. A navigation-based application contains a UI that allows users to drill into a hierarchy of items. It has a navigation bar at the top of the screen showing the item that is currently pushed into a stack. Figure 8-2 shows a good example of a navigation-based application — Settings.

Figure 8-2

Figure 8.2. Figure 8-2

In the Settings application, you can drill down into the settings of an application by selecting the item and navigating to another screen. When you want to go back to the previous screen, you simply touch the little button at the top-left corner of the screen to navigate back.

Hence, this chapter teaches you how to create these two types of applications using the templates provided by the iPhone SDK.

TAB BAR APPLICATIONS

Up until this point, you have seen the use of two types of application templates provided by the iPhone SDK: View-based Application and Window-based Application. The View-based Application template is the easiest to get started while the Window-based Application template provides the skeleton of an iPhone application and lets you create everything by yourself.

For building Tab Bar applications, you can either use the Window-based Application template, or more conveniently, use the Tab Bar Application template provided.

The following Try it Out uses the Tab Bar Application template to create a project and understand the underlying architecture. Download the necessary project files as indicated here.

Adding Tab Bar Items

The previous section demonstrated how to create a Tab Bar application using the template provided by the SDK. By default, the application template includes only two Tab Bar items, so in this section, you see how to add more Tab Bar items to the existing Tab Bar.

Displaying Tab Bar Applications in Landscape Mode

In Chapter 6, you learn that screen rotations in iPhone applications can be easily supported by overriding the shouldAutorotateToInterfaceOrientation: method. To support all screen orientations, you simply need to return a YES for this method, like this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
    interfaceOrientation {
    return YES;
}

Doing so causes the view to automatically rotate to support the new orientation. For Tab Bar applications, things are a little special. To see how different it is, go ahead and make the following modifications to the FirstViewController.m file:

//---FirstViewController.m---
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
    interfaceOrientation {
    return YES;
}

Press Command-R to test the application and change the screen rotation. What do you notice? Well, the orientation of the application does not change — it is still displayed in portrait mode. In order to support different orientations, all the View Controllers contained within the UITabBarController object must support the same orientations. That is to say, if one View Controller supports landscape left mode, then all the View Controllers must also support landscape left mode.

Hence, in the following Try It Out, you modify the application to support all orientations.

NAVIGATION-BASED APPLICATIONS

Tab Bar applications are suitable for situations in which you have several views, each view displaying different information so the user can quickly switch among them. However, sometimes you have hierarchical data that requires users to move from one screen to another based on what they have selected. In this situation, it is better to use a Navigation-based application.

To see how a Navigation-based application works, you can create one now, in the following Try It Out.

Navigating to Another View

The true use of a Navigation-based application is to navigate from one view to another. Therefore, this section shows you how to modify the application so that when the user touches a movie, the application navigates to another view.

SUMMARY

In this chapter, you have seen the two major types of iPhone applications supported by the SDK — Tab Bar applications and Navigation-based applications. Understanding how these two types of applications work will allow you to build multi-view applications that look just like those available on your iPhone.

EXERCISE

  1. Create a Tab Bar application with two Tab Bar items. When the user taps on the second Tab Bar item, display a list of movie titles.

  • WHAT YOU HAVE LEARNED IN THIS CHAPTER

TOPIC

KEY CONCEPTS

Creating a Tab Bar application

Use a UITabBarController instead of the usual UIViewController.

Determining which view is loaded first in a Tab Bar application

Change the sequence of View Controllers listed in the UITabBarController instance.

Adding Tab Bar Items to a Tab Bar application

After the Tab Bar item has been added to the Tab Bar, be sure to set its NIB Name property to a XIB file in your project. You also need to set its Class property to a View Controller class.

Supporting orientation change in a Tab Bar application

Ensure that all View Controllers implement the shouldAutorotateToInterfaceOrientation: method.

Navigating to another View Controller in a Navigation-based application

[self.navigationController
    pushViewController:self.detailsViewController
           animated:YES];
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.233.44