iOS中UINavigationController控制器使用详解
一、概述
UINavigationController用来管理视图控制器,在多视图控制器中常用。它以栈的形式管理视图控制器,管理视图控制器个数理论上不受限制(实际受内存限制),push和pop方法来弹入弹出控制器,最多只能显示一个视图控制器,那就是处于栈顶的视图控制器。
一般情况下,UINavigationController最少管理一个控制器,即最少有一个根视图控制器或者叫做栈底视图控制器。当然也有例外,如果不给它添加视图控制器也不会报错,界面上也有视图,因为UINavigationController继承自UIViewController,也有自己的view,只不过默认情况下.view.backgroundColor为nil,即透明的。
二、常用函数
- 使用
push方法能将某个控制器压入栈
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
- 使用
setViewControllers一次压入多个控制器vc1->vc2->vc3,会显示最后的控制器vc3(处于栈顶),代码如下:
UINavigationController *nav = [[UINavigationController alloc] init];
window.rootViewController = nav;
// 创建3个测试控制器
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor blueColor];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.view.backgroundColor = [UIColor redColor];
UIViewController *vc3 = [[UIViewController alloc] init];
vc3.view.backgroundColor = [UIColor greenColor];
// 最终会显示vc3
[nav setViewControllers:@[vc1,vc2,vc3] animated:YES];
- 使用pop方法可以移除栈顶控制器
当一个控制器被pop后,控制器内存就被释放了(会调用deinit/dealloc函数):
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
一层一层的返回不方便,可以直接回到指定的控制器VCA(处与VCA与栈顶之间的控制器全被释放),下面代码执行后,VC_A处于栈顶:
- (NSArray *)popToViewController:VC_A animated:(BOOL)animated;
回到根控制器(栈底控制器):
-(NSArray *)popToRootViewControllerAnimated:(BOOL)animated;
- 获取控制器
/// 当前管理的所有的控制器
@property(nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers; /// 栈顶控制器
@property(nullable, nonatomic,readonly,strong) UIViewController *topViewController; /// 当前可见的VC,可能是topViewController,也可能是当前topViewController present(modal)出来的VC,总而言之就是可见的VC
@property(nullable, nonatomic,readonly,strong) UIViewController *visibleViewController;
注意,topViewController与visibleViewController大部分情况一样,也有可能不同
三、导航条
UINavigationController是做导航用的,具体的操作大部是由导航条来完成,导航条的使用就显得很重要。导航条的内容由控制器的navigationItem属性决定。
1 navigationItem的属性
一般使用self.navigationItem.对应属性来获取属性,或者设置属性。或者使用self.navigationController获取到navigationController,再通过navigationController获取到想要设置的viewController
- 中间的标题文字
@property(nullable, nonatomic,copy) NSString *title;
- 中间标题视图
@property(nullable, nonatomic,strong) UIView *titleView;
- 导航栏附加解释说明,如果设置了此字段,导航栏会高出30个点显示此字段在
title正上方
@property(nullable,nonatomic,copy) NSString *prompt;

- 自定义左上角的返回按钮
/// 直接设置
@property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;
大部分情况下,我们需要指定左边返回按钮距离左边框的距离,可以如下设定:
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"gobackItem.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backViewcontroller)]; UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
// 设置边框距离,个人习惯设为-16,可以根据需要调节
fixedItem.width = -;
self.navigationItem.leftBarButtonItems = @[fixedItem, leftItem];
下图为设置边框前后的差别:


- 子导航条后退按钮,假设通过VC1
pushVC2,那么如果设置VC1.navigationItem.backBarButtonItem就会显示在VC2的左上角返回按钮;如果再设置VC2.navigationItem.leftBarButtonItem则会覆盖VC1的设置;如果VC1和VC2都没有设置,则会显示默认的backBarButtonItem。
@property(nullable,nonatomic,strong) UIBarButtonItem *backBarButtonItem;
- 自定义右上角的按钮,或多个按钮
@property(nullable, nonatomic,strong) UIBarButtonItem *rightBarButtonItem;
/// 一次设置多个按钮
@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *rightBarButtonItems;
2 设置navigationItem的字体格式
// 字体大小19,颜色为白色
[nav.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:],NSForegroundColorAttributeName:[UIColor whiteColor]}];
四、UIToolBar
UINavigationController自带了一个工具栏,通过[self.navigationController setToolbarHidden:NO];来显示工具栏,工具栏中的内容可以通过viewController的toolbarItems来设置,显示的顺序和设置的NSArray中存放的顺序一致,每一个UIBarButtonItem对象都可以设定点击事件,可以使用系统提供的很多常用风格的对象,也可以根据需求进行自定义,下面举例使用系统提供的样式。
// 1 显示工具条
[self.navigationController setToolbarHidden:NO];
// 2 创建四个UIBarButtonItem
UIBarButtonItem *itemOne = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
UIBarButtonItem *itemTwo = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:nil action:nil];
UIBarButtonItem *itemThree = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
UIBarButtonItem *itemFour = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:nil action:nil];
// 间隙
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// 3 添加到toolbarItems
vc.toolbarItems = @[itemOne,flexibleItem,itemTwo,flexibleItem,itemThree,flexibleItem,itemFour];
效果如下:

另外,UIToolBar使用的比较少,大部分情况下而是使用另一个导航控制器UITabBarController
五 UINavigationBar、UINavigationItem、UIToolbar与UIBarButtonItem四者关系
NavigaitonBar是导航栏,位于屏幕的上方,管理整个NavigationController的navigationItem,它类似navigationcontroller一样提供了一个栈来管理UINavigationItem,在编程时,一般只设置每个控制器的navigationItem属性- 一个导航控制器管理多个视图控制器(多个视图控制器共享一个导航控制器),而一个导航控制器只有一个
UINavigationBar,被管理的多个视图控制器共享这一个UINavigationBar,只要一个视图控制器改变了UINavigationBar的属性则影响是全局的。每个视图控制器都会有属于自己的UINavigationItem,系统会以懒加载的方式创建一个UINavigationItem显示在UINavigationBar中,改变UINavigationItem只会在当前控制器起作用,不会影响其它控制器。 Toolbar显示在屏幕底部,是导航控制器的工具栏,一个导航控制器只有一个,在任何被管理的视图控制器地方改变则会都改变。可以一次性添加多个UIBarButtonItem或按钮(包装成UIBarButtonItem后添加),有一个items数组属性。UIBarButtonItem是UINavigationItem或者Toolbar具体的一个按钮。
六、UINavigationControllerDelegate
有两个常用的方法
// 一般用于传递参数,或者做一些其它处理
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated; - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
参考:http://www.cnblogs.com/mddblog/p/4556974.html
iOS中UINavigationController控制器使用详解的更多相关文章
- iOS中MVC等设计模式详解
iOS中MVC等设计模式详解 在iOS编程,利用设计模式可以大大提高你的开发效率,虽然在编写代码之初你需要花费较大时间把各种业务逻辑封装起来.(事实证明这是值得的!) 模型-视图-控制器(MVC)设计 ...
- iOS中线程同步基本详解
为什么使用线程同步技术:多个线程是同时执行的 如果多个线程同时操作一个资源 会造成此资源的数据错乱 线程同步简介 线程同步,多条线程按顺序地访问某个资源 注意:此处的同步不是一起执行的意思 是一个一个 ...
- IOS中的NSTimer定时器详解
/* 在IOS中有多种定时器,这里我对NSTimer定时器做了一个简单的介绍.如果你是小白,你可能会从这篇文章中学习到一些知识,如果你是大牛,请别吝啬你的评论,指出我的不足,你的质疑是对我最大的帮助. ...
- iOS中 CoreGraphics快速绘图(详解) 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 第一步:先科普一下基础知识: Core Graphics是基于C的API,可以用于一切绘图操作 Core Graph ...
- iOS中 蓝牙2.0详解/ios蓝牙设备详解 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博 整体布局如下: 程序结构如右图: 每日更新关注:http://weibo.com/hanjunqiang ...
- iOS中--NSArray调用方法详解 (李洪强)
下面的例子以 NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"to ...
- IOS中的网络编程详解
在移动互联网时代,几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图,只有通过网络跟外界进行数据交互.数据更新,应用才能保持新鲜.活力,如果没有了网络,也就缺少了数据变化,无论外观多 ...
- IOS 中列表的TableView 详解,常用方法整理
一.创建一个列表,不管代码还是nib拖拉,在nib创建的时候,记得加他的二个代理 (UITableViewDelegate UITableViewDataSource) 代码创建的话,需要关联他的代理 ...
- IOS中UIActionSheet使用方法详解
一.初始化方法 - (instancetype)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)de ...
随机推荐
- 7z命令行参数中的路径
最近在自动化的过程中用到了7z命令行工具,发现其参数中的路径挺有意思的,在此总结一下.本文中所有demo使用的7z版本为:15.14 x64. 压缩某个文件夹 下面的命令会把g:\temp\目录和目录 ...
- STM32 DMA使用详解
DMA部分我用到的相对简单,当然,可能这是新东西,我暂时还用不到它的复杂功能吧.下面用问答的形式表达我的思路. DMA有什么用? 直接存储器存取用来提供在外设和存储器之间或者存储器和存储器之间的高速数 ...
- 【转载】css3 content 生成内容
content一般和:before,:after一起使用,用来生成内容(img和input没有该属性),content的内容一般可以为以下四种: none: 不生成任何值. attr: 插入标签属性值 ...
- Entity Framework 实体框架的形成之旅--实体框架的开发的几个经验总结
在前阵子,我对实体框架进行了一定的研究,然后把整个学习的过程开了一个系列,以逐步深入的方式解读实体框架的相关技术,期间每每碰到一些新的问题需要潜入研究.本文继续前面的主题介绍,着重从整体性的来总结一下 ...
- HoverTree.Model.ArticleSelect类的作用
ArticleSelect类在命名空间HoverTree.Model中可以认为是文章查询条件类,用于存放查询文章时的条件,例如HvtId就是文章的id.HvtIsShow就是文章的显示属性,当为-1是 ...
- div css仿京东订单流程图样式代码
效果展示 http://hovertree.com/texiao/css/25/ 本效果适合PC,也适合移动端 手机扫描二维码查看效果: 效果图: 代码如下: <!DOCTYPE html> ...
- sql中 in , not in , exists , not exists效率分析
in和exists执行时,in是先执行子查询中的查询,然后再执行主查询.而exists查询它是先执行主查询,即外层表的查询,然后再执行子查询. exists 和 in 在执行时效率单从执行时间来说差不 ...
- Delphi Berlin 10.1 for iOS 成生 info.plist 顺序改变了
在 Delphi Seattle 10 update 1 版本(含之前版本),只要 Project > Build 会立即生成 info.plist 如果需要修改 info.plist 可以利用 ...
- Typecho 新浪登陆插件 Sinauth
花了点时间弄了一个插件. 代码地址:https://github.com/web3d/plugins/tree/master/Sinauth Typecho的扩展机制还是比较完善的,可以自行增加Act ...
- 阿里巴巴开源技术 WebX
0. WebX项目目前已开源, 项目开源地址:https://github.com/webx/citrus-sample.git 项目参考文档:http://www.openwebx.org/docs ...