zoukankan      html  css  js  c++  java
  • NavigationController的使用

    1.创建

    通过xib创建

    通过代码创建

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

    2.导航栈

    有四个方法

      例如,想推进一个新的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”

    ps:最新发现, self.navigationItem.leftBarButtonItem 可以设置当前页面的左侧的按钮。这个需要再当前页面实现,而不是上一级页面。

    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/



    Wangkeke 2012-06-03 17:34 发表评论
  • 相关阅读:
    iptables详解
    Linux文件结构及基本文件夹
    linux的一些常用命令
    Sql Server REPLACE函数的使用
    MSSQL复制表操作
    MSSQL2005数据库显示单一用户模式,无法进行任何操作
    linux下查看所有用户及所有用户组
    SpringMVC基础-10-拦截器
    SpringMVC基础-09-文件上传(单文件、多文件上传)
    SpringMVC基础-08-数据转换 & 数据格式化 & 数据校验
  • 原文地址:https://www.cnblogs.com/cokecoffe/p/2537103.html
Copyright © 2011-2022 走看看