addChildViewController相关api深入剖析
注:本文根据个人的实践和理解写成,若有不当之处欢迎斧正和探讨!
addChildViewController是一个从iOS5开始支持的api接口,相关的一系列的接口是用来处理viewcontroller中嵌套显示其他viewcontroller的场景的。
在出现此api之前,大家可能会使用addsubview并持有viewcontroller对象的方式来实现这个需求,控制多个viewcontroller的view的hidden标签达到同时只显示1个子viewcontroller和切换子viewcontroller的目的。
相关主要api一览:
1.- addChildViewController:
加入子viewcontroller。
2.- removeFromParentViewController
将自身从父viewcontroller中移除(脱离关系)
子viewcontroller切换示例:
3. [self transitionFromViewController:self.currentViewController
toViewController:selectedVC
duration:0
options:UIViewAnimationOptionTransitionNone
animations:nil
completion:nil];
6. 一个只读属性:childViewControllers , 可以用来获取此容器viewcontroller当前拥有的全部childviewcontroller。
其中1和2很容易理解;就是建立/解除viewcontroller之间的父子关系(注意:实际使用时还是要结合调用addsubview来把视图加进来)。
3:当1个容器viewcontroller中加入多个child时,使用该api来切换childviewcontroller,并可以设置动画效果和结束事件处理,很方便。
其中4和5就有些奇怪了,光从命名来看可能会以为是子viewcontroller即将隐藏和已经隐藏时触发的方法,但实际上我们阅读其api发现情况不是这样。
看下苹果官方文档:
Called just before the view controller is added or removed from a container view controller.
Discussion
Your view controller can override this method when it needs to know that it has been added to a container.
If you are implementing your own container view controller, it must call the willMoveToParentViewController: method of the child view controller before calling the removeFromParentViewController method, passing in a parent value of nil.
When your custom container calls the addChildViewController: method, it automatically calls the willMoveToParentViewController: method of the view controller to be added as a child before adding it.
Called after the view controller is added or removed from a container view controller.
Discussion
Your view controller can override this method when it wants to react to being added to a container.
If you are implementing your own container view controller, it must call the didMoveToParentViewController: method of the child view controller after the transition to the new controller is complete or, if there is no transition, immediately after calling the addChildViewController: method.
The removeFromParentViewController method automatically calls the didMoveToParentViewController: method of the child view controller after it removes the child.
这里如果暂时先不考虑“implementing your own container view controller”,看一下苹果实现的container view controller, 最典型的莫过于 navigationcontroller了。
那么我们这里可以联想一下,可能navigationcontroller相关的pop、push也是基于这一套api机制来实现的。
我们在demo里面打印通过navigationcontroller push出来的界面的生命周期方法(包括willMoveToParentViewController和didMoveToParentViewController),
我们发现:
界面push进来时:
willMoveToParentViewController
viewdidload
didMoveToParentViewController
界面退出时:
willMoveToParentViewController
didMoveToParentViewController
dealloc
再结合上面api的两句话:
Called just before the view controller is added or removed from a container view controller. 在viewcontroller被添加或者移除之前时被调用;
Called after the view controller is added or removed from a container view controller. 在viewcontroller被添加或者移除之后被调用。
再来看“implementing your own container view controller”时的情况,我们仅仅通过1、2、3来使用这一套api,那么实际测试发现当child被加入到parent的时候只会触发willMoveToParentViewController,而当child被移出parent的时候只触发了didMoveToParentViewController方法。
再仔细看下苹果api文档说明:
在“implementing your own container view controller”时,将自己从父viewcontroller移除之前,需要手动调用willMoveToParentViewController;
同样的,将自己加入父viewcontroller以后(或者从其他子viewcontroller页面迁移而来时)需要手动调用didMoveToParentViewController(时机在addchild或者迁移的completion结束以后)。
综上,我们可以分析知:willMoveToParentViewController、didMoveToParentViewController可以用来帮助我们进行一些页面跳转相关的生命周期业务逻辑处理,但其存在一些问题不可不察:
1.需要准确的理解这两个方法触发的时机
2.对于系统的container,这两个方法一定会在适当的时机触发;而对于自己实现的container,必须要在适当的时机手动调用这两个方法,才能保证其触发的完备性。如果你不手动调用,那么你也要准确的理解在这种场景下什么时候会触发
3.从一个开发者角度来说,我认为这两个api的设计比较失败。首先其命名并不能直观表达出其触发场景;且其使用上也有诸多限制。因此对于调用者来说,建议仅在不使用这两个api就无法满足业务需求时才使用这两个api。
最后:推荐一个国人的UI框架,写的很用心,其中addChildViewController相关api还有其他一些转场之类的api使用,可以作为很好的学习参考。 https://github.com/tianzhuo112/VTMagic
addChildViewController相关api深入剖析的更多相关文章
- OpenGL FrameBufferCopy相关Api比较(glCopyPixels,glReadPixels,glCopyTexImage2D,glFramebufferTexture2D)
OpenGL FrameBufferCopy相关Api比较 glCopyPixels,glReadPixels,glCopyTexImage2D,glFramebufferTexture2D 标题所述 ...
- QQ音乐的各种相关API
QQ音乐的各种相关API 分类: oc2014-01-29 15:34 2676人阅读 评论(2) 收藏 举报 基本上论坛里做在线音乐的都在用百度的API,进来发现百度的API不仅歌曲的质量不可以保证 ...
- [原创]java WEB学习笔记44:Filter 简介,模型,创建,工作原理,相关API,过滤器的部署及映射的方式,Demo
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- 关于iOS中用AudioFile相关API解码或播放AAC_HE_V2时仅仅能识别单声首22.05k採样率的问题
关于iOS中用AudioFile相关API解码或播放AAC_HE_V2时仅仅能识别单声首22.05k採样率的问题 在官方AQPlayer Demo 和 aqofflinerender中.都用了Audi ...
- TCP/IP协议栈源码图解分析系列10:linux内核协议栈中对于socket相关API的实现
题记:本系列文章的目的是抛开书本从Linux内核源代码的角度详细分析TCP/IP协议栈内核相关技术 轻松搞定TCP/IP协议栈,原创文章欢迎交流, byhankswang@gmail.com linu ...
- 【Socket编程】Java中网络相关API的应用
Java中网络相关API的应用 一.InetAddress类 InetAddress类用于标识网络上的硬件资源,表示互联网协议(IP)地址. InetAddress类没有构造方法,所以不能直接new出 ...
- java 11 移除的一些其他内容,更简化的编译运行程序,Unicode 10,移除了不太使用的JavaEE模块和CORBA技术,废除Nashorn javascript引擎,不建议使用Pack200 相关api
移除的一些其他内容 移除项 移除了com.sun.awt.AWTUtilities 移除了sun.misc.Unsafe.defineClass, 使用java.lang.invoke.MethodH ...
- python 以单例模式封装logging相关api实现日志打印类
python 以单例模式封装logging相关api实现日志打印类 by:授客QQ:1033553122 测试环境: Python版本:Python 2.7 实现功能: 支持自由配置,如下lo ...
- 029 RDD Join相关API,以及程序
1.数据集 A表数据: 1 a 2 b 3 c B表数据: 1 aa1 1 aa2 2 bb1 2 bb2 2 bb3 4 dd1 2.join的分类 inner join left outer jo ...
随机推荐
- 算法教程(2)zz
In the previous section we saw how to use vectors to solve geometry problems. Now we are going to le ...
- Catching Fish[HDU1077]
Catching Fish Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- TAT,我的LCT转双旋了
这里是rotate和splay函数 void rotate(int x) { ]==x; ch[y][d^]=ch[x][d];pre[ch[x][d]]=y; ch[z][ch[z][]==y]=x ...
- JavaScript事件大全3
//无模式的提示框 //屏蔽按键 <html> <head> <meta http-equiv="Content-Type" content=& ...
- 将textField编辑完内容作为参数发送请求
将textField编辑完内容作为参数发送请求 首先赋值默认值 其次把编辑完的内容传给model,这样的话,model里面的数据就是编辑完之后的内容了
- python深浅拷贝
赋值引用,多变量使用同内存.对于可变数据对象,修改其中一个,影响其他.浅拷贝,只拷贝数据父对象,不拷贝其中子对象.深拷贝,拷贝对象及其子对象. 赋值引用:(修改,所有多影响) list1=[1,2,3 ...
- 五、Pillar数据管理中心
Pillar是数据管理中心. Pillar在saltstack中主要作用是存储和定义一些配置管理中需要的信息(比如:软件版本,用户名,密码等) 修改pillar相关配置文件: [root@super6 ...
- 移动web app开发小贴士 收藏有用
1 创建主屏幕图标 (Creating a home screen icon ,for ios) 1 2 3 4 5 6 //57*57 <link rel="apple-touc ...
- 支持nmap批量漏洞扫描的script
NSE脚本,会在NMAP扫描时,通过-sV发现的端口详细信息对应到CVE相应的漏洞: nmap -sS -sV --script=vulscan www.scip.ch -p25 下面是该脚本的说明和 ...
- Jenkins+Maven+Svn搭建持续集成环境持续集成和自动部署
Jenkins和Hudson有很深的渊源,Jenkins目前更新频繁,目前选用Jenkins为持续集成工具和自动部署 Jenkins的使用有很多的介绍,主要记录如下要点: 192.168.1.240: ...