一、概述

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. 博客已经迁移至http://www.xuechenlei.com

    博客园是个非常好的公共平台,这里总有我想找的东西,本着人生在于折腾的精神和一些个性化的定制,将博客内容迁移到www.xuechenlei.com,时而同步分享到这里,欢迎访问关注:

  2. Chrome开发者工具详解(1)-Elements、Console、Sources面板

    Chrome开发者工具详解(1)-Elements.Console.Sources面板 Chrome开发者工具面板 面板上包含了Elements面板.Console面板.Sources面板.Netwo ...

  3. ES6笔记(6)-- Set、Map结构和Iterator迭代器

    系列文章 -- ES6笔记系列 搞ES6的人也是够无聊,把JS弄得越来越像Java.C++,连Iterator迭代器.Set集合.Map结构都出来了,不知道说什么好... 一.简单使用 1. iter ...

  4. 各大IT公司校园招聘程序猿笔试、面试题集锦

    转自:http://blog.csdn.net/hackbuteer1/article/details/7959921#t4 百度一面 1.给定一个字符串比如“abcdef”,要求写个函数编程“def ...

  5. asp.net MVC4的执行流程

    MVC在底层和传统的asp.net是一致的,在底层之上,相关流程如下: 1)Global.asax里,MvcApplication对象的Application_Start()事件中,调用 RouteC ...

  6. 数据库中char, varchar, nvarchar的差异

    char     char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值.当你输入的字符大于指定的数时,它会截取超出的字符.    nvarc ...

  7. winform 判断鼠标是否在按钮控件范围内

    public void MourseLeave() { bool b = this.RectangleToScreen(this.ClientRectangle).Contains(MousePosi ...

  8. Web前端:HTML~CSS~JS

    网页主要由3部分组成:结构.表现.行为.目前网页的新标准是W3C,模式是HTML.CSS.JavaScript,这是前端开发最核心的3个技术.前2个技术的最新版本分别为HTML5.CSS3.  “HT ...

  9. 使用Monkey进行压力测试

    Android可以使用Monkey向应用发送一连串的随机操作,就好像把手机交给一只猴子让它任意操作一样,以此来检测应用是否健壮,是否容易出错或崩溃.操作的类型包括触屏.移动.按键等. Monkey的语 ...

  10. 非阻塞同步算法与CAS(Compare and Swap)无锁算法

    锁(lock)的代价 锁是用来做并发最简单的方式,当然其代价也是最高的.内核态的锁的时候需要操作系统进行一次上下文切换,加锁.释放锁会导致比较多的上下文切换和调度延时,等待锁的线程会被挂起直至锁释放. ...