1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

9  Implementing Navigation Bars

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (8.37 MB, 640 trang )


@synthesize window;

@synthesize navigationController;

- (BOOL) application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

FirstViewController *controller =

[[FirstViewController alloc]

initWithNibName:@"FirstViewController"

bundle:nil];

UINavigationController *theNavigationController =

[[UINavigationController alloc]

initWithRootViewController:controller];

self.navigationController = theNavigationController;

[theNavigationController release];

[self.navigationController setNavigationBarHidden:NO

animated:YES];

[controller release];

// Add the view controller's view to the window and display.

[window addSubview:self.navigationController.view];

[window makeKeyAndVisible];

}



return YES;



- (void)dealloc {

[navigationController release];

[window release];

[super dealloc];

}

@end



Discussion

There are two good ways to create navigation controllers in an application for the

iPhone or iPad:

• Creating a navigation-based application

• Creating a UINavigationController object manually in the application delegate

I choose the second method over the first, for various reasons. One is that I feel I get

more control over how the structure of my application is created from the beginning;

another is that the second method makes it easier for me to control the lifetime of the

navigation controller.



2.9 Implementing Navigation Bars | 55



www.it-ebooks.info



The first method also gives you control over navigation, but makes you

feel less like you can do anything you want.



Both methods will be described in this recipe.

To create a navigation-based application, follow these steps:

1. In Xcode, choose File→New Project.

2. In the project templates, choose Navigation-based Application on the righthand

side of the screen, as shown in Figure 2-11.

3. Click the Choose button.

4. Give your project a name and choose where you want to save it, as shown in

Figure 2-12.

5. Click Save and Xcode will create your project for you.



Figure 2-11. Creating a navigation-based application in Xcode



56 | Chapter 2: Implementing Controllers and Views



www.it-ebooks.info



Figure 2-12. Choosing a name for the project and saving it to disk



Now that you have created a navigation-based application, you might wonder how that

project template differs, for instance, from the utility application. Well, open the .h file

of your application’s delegate; you will see code similar to this:

#import

@interface MyProjectAppDelegate : NSObject {



}



UIWindow *window;

UINavigationController *navigationController;



@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet UINavigationController

*navigationController;

@end



Xcode created a navigation controller for your delegate (which, as I mentioned before,

is the preferred place to create your navigation controllers). Now you can go ahead and

add view controllers to this navigation controller (I will explain this process shortly).



2.9 Implementing Navigation Bars | 57



www.it-ebooks.info



The other way to create a navigation-based application is to use any application template you want and create your own navigation controller. For instance:

1. In Xcode, choose File→New Project.

2. From the project templates, choose the View-based Application template, as

shown in Figure 2-13.

3. Click the Choose button.

4. Choose a name and location for your project, as shown in Figure 2-14.

5. Click the Save button.



Figure 2-13. Creating a view-based application in Xcode



Now you need to find your application’s delegate. If you named your project “X,” the

application’s delegate by default will be called XAppDelegate. The project created in

Figure 2-14 is called Project3; therefore, the application delegate is called Project3App

Delegate. Once you find the delegate, open the .h file first and add a navigation controller to it:

#import



58 | Chapter 2: Implementing Controllers and Views



www.it-ebooks.info



@class Project3ViewController;

@interface Project3AppDelegate : NSObject

{

@public

UIWindow

*window;

Project3ViewController *viewController;

UINavigationController *navigationController;

}

@property (nonatomic, retain)

IBOutlet UIWindow *window;

@property (nonatomic, retain)

IBOutlet Project3ViewController *viewController;

@property (nonatomic, retain)

UINavigationController *navigationController;

@end



Figure 2-14. Choosing a name for the view-based application



2.9 Implementing Navigation Bars | 59



www.it-ebooks.info



Open the application delegate’s .m file and create your navigation controller:

#import "Project3AppDelegate.h"

#import "Project3ViewController.h"

@implementation Project3AppDelegate

@synthesize window;

@synthesize viewController;

@synthesize navigationController;

- (BOOL)

application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

UINavigationController *theNavigationController =

[[UINavigationController alloc]

initWithRootViewController:self.viewController];

self.navigationController = theNavigationController;

[theNavigationController release];

[self.navigationController setNavigationBarHidden:NO

animated:YES];

[window addSubview:self.navigationController.view];

[window makeKeyAndVisible];

}



return YES;



- (void)dealloc {

[navigationController release];

[viewController release];

[window release];

[super dealloc];

}

@end



You must release the navigation controller whenever you are done with

it. Some programmers prefer to release the navigation controller in the

applicationWillTerminate delegate message received by the application

delegate. But you might find it more convenient to release the navigation

controller elsewhere.



See Also

Recipe 2.10; Recipe 2.11



60 | Chapter 2: Implementing Controllers and Views



www.it-ebooks.info



2.10 Switching from One View to Another

Problem

You have two or more view controllers and a navigation controller. You want to switch

from one view controller to the other.



Solution

If you already have a navigation controller, you are halfway there. If not, you need to

create a navigation controller first (see Recipe 2.9). Once you have a navigation controller, you can use the pushViewController: method to push another view controller

onto the hierarchy:

- (void) goToSecondViewController{

SecondViewController *controller =

[[SecondViewController alloc]

initWithNibName:@"SecondViewController"

bundle:nil];

[self.navigationController pushViewController:controller

animated:YES];

[controller release];

}

- (void) viewDidLoad{

[super viewDidLoad];

/* Show the second View Controller 3 seconds

after this View Controller's

view is loaded */

[self performSelector:@selector(goToSecondViewController)

withObject:nil

afterDelay:3.0f];

}

- (void) viewDidUnload{

[super viewDidUnload];



}



[NSObject

cancelPreviousPerformRequestsWithTarget:self

selector:@selector(goToSecondViewController)

object:nil];



2.10 Switching from One View to Another | 61



www.it-ebooks.info



This code runs on a view controller and attempts to push a view controller of class

SecondViewController into the stack maintained by the navigation controller, three

seconds after the first view controller’s view is loaded.



Discussion

The pushViewController:animated: method of the navigation controller takes two parameters. One is the view controller to be pushed onto the stack. The other specifies

whether to animate the transition between the current top view controller and the new

view controller getting pushed onto the stack (a Boolean value).

Once the new view controller is pushed onto the stack, the view associated with that

view controller will get shown as the top view, and that view controller will become

the topmost view controller on the stack.

The title property of an instance of the UIViewController sets the title of the navigation bar of a navigation controller displaying that view controller. A tab bar also uses

this property, but we will discuss tab bars in Recipe 2.16.



See Also

Recipe 2.9; Recipe 7.4



2.11 Setting the Title on a Navigation Bar

Problem

You want your navigation bar to have a title.



Solution

Each view controller is represented on a navigation controller by a navigation item.

Think of a navigation item as a placeholder for the current view controller (the owner

of the navigation item) on the navigation controller. Therefore, every instance of the

UIViewController class comes with a property named navigationItem. You can access

the title property of the navigationItem in order to set the title of the current view

controller on the navigation controller:

self.navigationItem.title = @"My View Controller";



Discussion

The navigation item controls the buttons and the title that appear on the navigation

controller, and these items are specific to the top view controller of the navigation

controller. Once a new view controller is pushed onto the stack, the navigation bar will

automatically update itself to reflect the changes requested by the new top view controller, such as the title of the navigation bar and right and left navigation bar buttons.



62 | Chapter 2: Implementing Controllers and Views



www.it-ebooks.info



Xem Thêm
Tải bản đầy đủ (.pdf) (640 trang)

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×