UINavigationController

本文详细介绍了UINavigationController和UINavigationBar的概念、作用及使用方法,包括如何创建UINavigationController实例、获取当前顶部UIViewController和UINavigationBar配置等内容。

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 *)application
didFinishLaunchingWithOptions:(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 *)aTableView
didSelectRowAtIndexPath:(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];
}
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值