NavigationController的使用

来源:互联网 发布:赢在大数据系列丛书 编辑:程序博客网 时间:2024/06/04 01:24

NavigationController的使用


1.创建

通过xib创建

通过代码创建

一个UINavigationcontroller包括 navigation bar,可选的navigation toolbar,RootViewController.

2.导航栈

有四个方法

  • – pushViewController:animated:
  • – popViewControllerAnimated:
  • – popToRootViewControllerAnimated:
  • – popToViewController:animated:
    例如,想推进一个新的viewcontroller,到导航栈中,代码:
- (void)tableView:(UITableView *)tableView        didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    [[tableView cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];//1.     DetailsViewController *detailsViewController = [[DetailsViewController alloc]        initWithNibName:@"DetailsViewController" bundle:nil];    [self.navigationController pushViewController:detailsViewController];    [detailsViewController release];}
    这里有两个需要注意的地方
    1.进入下一个页面的时候,table中的选择行要取消。
    2.记得release要push的controller.因为导航栈是retain的。

3.配置Navigation bar

可能大家想直接访问navigationcontroller 的navigation bar。但是通常我们不这样做。而是维护每个viewcontroller的 navigation item。

这里不要将navigation item 与 navigation bar 混淆,navigation item不是UIView的子类。它是一个用来更新navigtion bar的存储信息的类。

还是上代码说明:

- (void)tableView:(UITableView *)tableView        didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  [[tableView cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];   Person *person;   // Some code that sets person based on the particular cell that was selected   DetailsViewController *detailsViewController = [[DetailsViewController alloc]    initWithNibName:@"DetailsViewController" bundle:nil];  detailsViewController.navigationItem.title = person.name;  [self.navigationController pushViewController:detailsViewController];  [detailsViewController release];}
detailsViewController.navigationItem.title = person.name;这句话的意思就是把二级界面的导航标题设置成person.name

要注意两点:1.我们并没有直接操作navigation bar 2.在push 新的controller之前设置标题

当新的detailcontroller被push后,UINavigationController会自动更新navigation bar。

4.返回按钮

默认情况下,当你将一个新的viewcontroller推入栈的时候,返回按钮将显示前一个页面的controller的 navigation item的title。

如果想定制返回按钮的标题还有事件的话,可以用以下代码。

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"      style:UIBarButtonItemStylePlain target:nil action:nil];self.navigationItem.backBarButtonItem = backButton;[backButton release];

注意,这里的self是第一级的view controller。这样的话第二级的页面将显示“Back”

5.左右按钮

navigation item还有两个属性leftBarButtonItem rightBarButtonItem。

一般leftBarButtonItem只出现在RootviewController中使用,因为其他页面一般都显示一个返回按钮。

UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithTitle:@"Settings"      style:UIBarButtonItemStylePlain target:self action:@selector(handleSettings)];self.navigationItem.rightBarButtonItem = settingsButton;[settingsButton release];

这会在右侧添加一个“Setting”的按钮,并触发handleSetting事件。

6.在首页隐藏Navigation Bar

在RootViewController.m中实现如下:
- (void)viewWillAppear:(BOOL)animated {[super viewWillAppear:animated][self.navigationController setNavigationBarHidden:YES animated:YES];} - (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:animated][self.navigationController setNavigationBarHidden:NO animated:YES];}

这篇文章翻译自http://www.iosdevnotes.com/2011/03/uinavigationcontroller-tutorial/