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/
- NavigationController的使用
- NavigationController的使用
- NavigationController的使用
- NavigationController的使用
- NavigationController的基本使用
- Swift NavigationController的使用
- IOS navigationController.popToViewController的使用
- 非常详细的 navigationController 的使用
- iOS使用NavigationController经常出现的问题
- iOS基础: navigationController.popToViewController的使用
- navigationController的创建和基本使用
- 使用NavigationController的一个小技巧
- IOS开发中 navigationController.popToViewController的使用
- iOS使用NavigationController经常出现的问题
- NavigationController的使用及下一级页面标题的设置
- NavigationController的使用及下一级页面标题的设置
- 使用MBProgressHUD引发"self.navigationController.view为nil"的血案
- TabBarController与NavigationController结合使用时的隐藏问题
- 如何倒序输出整数(如输入:123;输出:321)
- DB2数据库的启动与关闭
- 在 XenServer 上为 C1K New UPS 安装 WinPower 控制软件
- 线段树总结
- 字符串操作小结
- NavigationController的使用
- Ubuntu快速显示桌面的方法
- hdu3007(最小覆盖圆问题)
- 设计模式
- php关联数组,20行搞定约瑟夫问题
- 寻找波谷
- 【游戏引擎】深入分析Cocos2d-x 2.0中的“纹理”(四)
- 不在Activity类中(没有context)怎么删除动态控件?
- 7.1 简单枚举---7.1.1除法