一、概述

UINavigationController用来管理视图控制器,在多视图控制器中常用。它以栈的形式管理视图控制器,管理视图控制器个数理论上不受限制(实际受内存限制),pushpop方法来弹入弹出控制器,最多只能显示一个视图控制器,那就是处于栈顶的视图控制器。

一般情况下,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];

下图为设置边框前后的差别:

  • 子导航条后退按钮,假设通过VC1pushVC2,那么如果设置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];来显示工具栏,工具栏中的内容可以通过viewControllertoolbarItems来设置,显示的顺序和设置的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是导航栏,位于屏幕的上方,管理整个NavigationControllernavigationItem,它类似navigationcontroller一样提供了一个栈来管理UINavigationItem,在编程时,一般只设置每个控制器的navigationItem属性
  • 一个导航控制器管理多个视图控制器(多个视图控制器共享一个导航控制器),而一个导航控制器只有一个UINavigationBar,被管理的多个视图控制器共享这一个UINavigationBar,只要一个视图控制器改变了UINavigationBar的属性则影响是全局的。每个视图控制器都会有属于自己的UINavigationItem,系统会以懒加载的方式创建一个UINavigationItem显示在UINavigationBar中,改变UINavigationItem只会在当前控制器起作用,不会影响其它控制器。
  • Toolbar显示在屏幕底部,是导航控制器的工具栏,一个导航控制器只有一个,在任何被管理的视图控制器地方改变则会都改变。可以一次性添加多个UIBarButtonItem或按钮(包装成UIBarButtonItem后添加),有一个items数组属性。
  • UIBarButtonItemUINavigationItem或者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控制器使用详解的更多相关文章

  1. iOS中MVC等设计模式详解

    iOS中MVC等设计模式详解 在iOS编程,利用设计模式可以大大提高你的开发效率,虽然在编写代码之初你需要花费较大时间把各种业务逻辑封装起来.(事实证明这是值得的!) 模型-视图-控制器(MVC)设计 ...

  2. iOS中线程同步基本详解

    为什么使用线程同步技术:多个线程是同时执行的 如果多个线程同时操作一个资源 会造成此资源的数据错乱 线程同步简介 线程同步,多条线程按顺序地访问某个资源 注意:此处的同步不是一起执行的意思 是一个一个 ...

  3. IOS中的NSTimer定时器详解

    /* 在IOS中有多种定时器,这里我对NSTimer定时器做了一个简单的介绍.如果你是小白,你可能会从这篇文章中学习到一些知识,如果你是大牛,请别吝啬你的评论,指出我的不足,你的质疑是对我最大的帮助. ...

  4. iOS中 CoreGraphics快速绘图(详解) 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 第一步:先科普一下基础知识: Core Graphics是基于C的API,可以用于一切绘图操作 Core Graph ...

  5. iOS中 蓝牙2.0详解/ios蓝牙设备详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 整体布局如下:     程序结构如右图: 每日更新关注:http://weibo.com/hanjunqiang  ...

  6. iOS中--NSArray调用方法详解 (李洪强)

    下面的例子以     NSArray *array = [NSArray arrayWithObjects:@"wendy",@"andy",@"to ...

  7. IOS中的网络编程详解

    在移动互联网时代,几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图,只有通过网络跟外界进行数据交互.数据更新,应用才能保持新鲜.活力,如果没有了网络,也就缺少了数据变化,无论外观多 ...

  8. IOS 中列表的TableView 详解,常用方法整理

    一.创建一个列表,不管代码还是nib拖拉,在nib创建的时候,记得加他的二个代理 (UITableViewDelegate UITableViewDataSource) 代码创建的话,需要关联他的代理 ...

  9. IOS中UIActionSheet使用方法详解

    一.初始化方法 - (instancetype)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)de ...

随机推荐

  1. Insert Plain Text and Images into RichTextBox at Runtime

    Insert Plain Text and Images into RichTextBox at Runtime' https://www.codeproject.com/Articles/4544/ ...

  2. 【转载】8天学通MongoDB——第三天 细说高级操作

    今天跟大家分享一下mongodb中比较好玩的知识,主要包括:聚合,游标. 一: 聚合 常见的聚合操作跟sql server一样,有:count,distinct,group,mapReduce. &l ...

  3. Angular 2 要来了,Wijmo 已准备好迎接

    Angular 是一款优秀的前端JS框架,已被用于Google的多款产品中,其核心特点是:MVVM.模块化.自动化双向数据绑定.语义化标签.依赖注入等.6年过去了,Angular 迎来了2.0版本. ...

  4. cros解决跨域

  5. Yii2框架打包成Phar包报错的经历

    以yii2为例 打包文件过程比较简单,但打包好以后简单测试yii命令,一直报错: PHP Fatal error: Uncaught yii\base\InvalidParamException: T ...

  6. springmvc(4)注解简单了解

    对于我这样的新人来说,因为是刚开始做项目,所以以前的技术不是用的很多,就比如springmvc来说,实际上使用的都是注解形式的,对于那些全部都是配置的来说,虽然也了解一些,但是实际上还是没试用过的. ...

  7. Scalaz(0) - 写在前面

    面向对象编程范畴(OOP)从80年代C++到90年代java的兴起已经经历了几十年的高潮,是不是已经发展到了尽头,该是函数式编程(FP)开始兴旺发达的时候了吧.这样说似乎心眼儿有点坏,可能会得罪当今大 ...

  8. 【局部特征】ASIFT

    由于相机正面白摄物体时,相机的光轴方向可能发生变化,带来扭曲.而SIFT算法虽具有完全的尺度不变性,但不具有完全的仿射不变性,对拍摄角度发生大角度空间变化的图像特征提取有一定的局限性.ASift通过模 ...

  9. 文件并发(日志处理)--队列--Redis+Log4Net

    多线程操作同一个文件时会出现并发问题.解决的一个办法就是给文件加锁(lock),但是这样的话,一个线程操作文件时,其它的都得等待,这样的话性能非常差.另外一个解决方案,就是先将数据放在队列中,然后开启 ...

  10. Python自动化测试工具Splinter简介和使用实例

    Splinter 快速介绍 官方网站:http://splinter.cobrateam.info/ 官方介绍: Splinter is an open source tool for testing ...