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 ...
随机推荐
- 人见人爱A^B
求A^B的最后三位数表示的整数.说明:A^B的含义是“A的B次方” Input 输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A= ...
- Maven中多模块的编译顺序
在多模块的工程中,如果模块之间存在依赖关系,那模块的编译必须要有顺序的要求.例如:P(parent)中包含A模块和B模块,且A模块依赖于B模块,那么在P中的pom,xml中需申明为: <modu ...
- LIS(nlogn) POJ 3903 Stock Exchange
题目传送门 题意:LIS最长递增子序列 O(nlogn) 分析:设当前最长递增子序列为len,考虑元素a[i]; 若d[len]<a[i],则len++,并使d[len]=a[i]; 否则,在d ...
- Quartz.Net 配置模板范例
1.App.config <?xml version="1.0" encoding="utf-8"?> <configuration& ...
- CentOS6.4 安装LVS-RRD监控LVS
1.安装依赖包 yum install -y php httpd bc rrdtool 启动apache (我看网上的一些文档说不能用80端口,但我用80端口试了一下也好使,如果出现不好使的情况就改一 ...
- 像jQuery那样,采用链式方法,封装一个方法:CSS()
主要思路就是:返回this对象,将所获取的操作元素放入一个数组中.在原型中添加拓展方法 <html> <head> <title></title> &l ...
- javascript 三个 对话框
用法: 一般写在 </html>之后,<script language="javascript">代码必须放在这里面</script> 三个常用 ...
- hdu Inverting Cups
这题需要分类讨论: 第一种情况: n为奇数m为偶数的情况无解,因为m为偶数,每次翻转将把从正面翻到反面的个数x减去从反面翻到正面的个数y,得到的数必定为偶数.因为x+y为偶数,x-y也为偶数.而总个数 ...
- 浅试 JNI编程
好吧,开始我的第一个JNI试验小程序 HelloWorld.java 代码清单 public class HelloWorld { static { System.loadLibrary(" ...
- [排错] Status error 2850
前几天重新搭建了APAC的Netbackup, 终于可以备份成功了, 但是今天在做还原的时候遇到了些小问题,记录下来. 1. 在CNHZSRV04BPO上执行还原的时候报下面的错误. 2. 经过检查发 ...