UINavigationController

来源:互联网 发布:薛之谦淘宝男装 编辑:程序博客网 时间:2024/05/10 22:15

When your application presents multiple screens of information, UINavigationController maintains a stack of those screens. The stack is an NSArray of view controllers, and each screen is the view of a UIViewController. When a UIViewController is on top of the stack, its view is visible.

When you initialize an instance of UINavigationController, you give it one UIViewController. This UIViewController is called the root view controller. The root view controller is always on the bottom of the stack.

The UIViewController that is currently on top of the stack is accessed by sending the message topViewController to the UINavigationControllerinstance. You can also get the entire stack as an NSArray by sending the navigation controller the message viewControllers.

The UINavigationController is also a subclass of UIViewController, so it has a view of its own. Its view always has at least two subviews: aUINavigationBar and the view of its topViewController.

复制代码
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions{  ItemsViewController *itemsViewController = [[ItemsViewController alloc] init];  // Create an instance of a UINavigationController  // its stack contains only itemsViewController  UINavigationController *navController = [[UINavigationController alloc]                                             initWithRootViewController:itemsViewController];  // You can now release the itemsViewController here,  // UINavigationController will retain it  [itemsViewController release];  // Place navigation controller's view in the window hierarchy  [[self window] setRootViewController:navController];  [navController release];  [[self window] makeKeyAndVisible];  return YES;}
复制代码

UINavigationBar

The UINavigationBar isn't very interesting right now. At a minimum, a UINavigationBar should display a descriptive title for the UIViewController that is currently on top of the UINavigationController's stack.

Every UIViewContoller has a navigationItem property of type UINavigationItem. However, unlike UINavigationBar, UINavigationItem is not a subclass of UIView, so it cannot appera on the screen. Instead, the navigation item supplies the navigation bar with the contentit needs to draw. When a UIViewController comes to the top of a UINavigationController’s stack, the UINavigationBar uses the UIViewController’s navigationItem to configure itself.

A navigation item can hold more than just a title string. There are three customizable areas for each UINavigationItem: a leftBarButtonItem, a rightBarButtonItem and a titleView. The left and right bar button items are  pointers to instances of UIBarButtonItem,which contains the information for a button that can only be displayed on a UINavigationBar or a UIToolbar.

Like UINavigationItem, UIBarButtonItem is not a subclass of UIView but supplies the content that a UINavigationBar needs to draw. Consider the UINavigationItem and its UIBarButtonItems to be containers for strings, images, and other content. A UINavigationBar knows how to look in those containers and draw the content it finds.

navigation with UINavigationController

复制代码
- (void)tableView:(UITableView *)aTableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{  ItemDetailViewController *detailViewController =  [[[ItemDetailViewController alloc] init] autorelease];  NSArray *possessions = [[PossessionStore defaultStore] allPossessions];  // Give detail view controller a pointer to the possession object in row  [detailViewController setPossession:  [possessions objectAtIndex:[indexPath row]]];  [[self navigationController] pushViewController:detailViewController                                          animated:YES];}
复制代码
原创粉丝点击