官方文档解读

// An array of children view controllers. This array does not include any presented view controllers.
// 子控制器数组,不包括任何已经显示的控制器
@property(nonatomic,readonly) NSArray<__kindof UIViewController *> *childViewControllers API_AVAILABLE(ios(5.0));/*If the child controller has a different parent controller, it will first be removed from its current parentby calling removeFromParentViewController. If this method is overridden then the super implementation mustbe called.如果子控制器拥有一个不同的父控制器,默认会先调用removeFromParentViewController将其移除如果重写该方法,必须调用[super addChildViewController:childController];
*/- (void)addChildViewController:(UIViewController *)childController API_AVAILABLE(ios(5.0));/*Removes the the receiver from its parent's children controllers array. If this method is overridden thenthe super implementation must be called.从其父控制器的子控制器数组中将其删除如果重写该方法,必须调用[super removeFromParentViewController];
*/- (void)removeFromParentViewController API_AVAILABLE(ios(5.0));/*This method can be used to transition between sibling child view controllers. The receiver of this method istheir common parent view controller. (Use [UIViewController addChildViewController:] to create theparent/child relationship.) This method will add the toViewController's view to the superview of thefromViewController's view and the fromViewController's view will be removed from its superview after thetransition completes. It is important to allow this method to add and remove the views. The arguments tothis method are the same as those defined by UIView's block animation API. This method will fail with anNSInvalidArgumentException if the parent view controllers are not the same as the receiver, or if thereceiver explicitly forwards its appearance and rotation callbacks to its children. Finally, the receivershould not be a subclass of an iOS container view controller. Note also that it is possible to use theUIView APIs directly. If they are used it is important to ensure that the toViewController's view is addedto the visible view hierarchy while the fromViewController's view is removed.该方法用于同级子控制器的切换(懒得翻译了)
*/
- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_SWIFT_DISABLE_ASYNC API_AVAILABLE(ios(5.0));/*These two methods are public for container subclasses to call when transitioning between childcontrollers. If they are overridden, the overrides should ensure to call the super. The parent argument inboth of these methods is nil when a child is being removed from its parent; otherwise it is equal to the newparent view controller.这两个方法是用于子控制器切换,如果重写该方法,记得调用super方法当子控制器从父控制器中移除的时候,参数设置为nil,否则则会成为新的父控制器addChildViewController: will call [child willMoveToParentViewController:self] before adding thechild. However, it will not call didMoveToParentViewController:. It is expected that a container viewcontroller subclass will make this call after a transition to the new child has completed or, in thecase of no transition, immediately after the call to addChildViewController:. Similarly,removeFromParentViewController does not call [self willMoveToParentViewController:nil] before removing thechild. This is also the responsibility of the container subclass. Container subclasses will typically definea method that transitions to a new child by first calling addChildViewController:, then executing atransition which will add the new child's view into the view hierarchy of its parent, and finally will calldidMoveToParentViewController:. Similarly, subclasses will typically define a method that removes a child inthe reverse manner by first calling [child willMoveToParentViewController:nil].addChildViewController在添加子控制器之前,会先调用willMoveToParentViewController方法,但是不调用didMoveToParentViewController方法同样removeFromParentViewController在移除子控制器之前,不会调用willMoveToParentViewControlle,但是会调用didMoveToParentViewController方法(懒得翻译了)*/
- (void)willMoveToParentViewController:(nullable UIViewController *)parent API_AVAILABLE(ios(5.0));
- (void)didMoveToParentViewController:(nullable UIViewController *)parent API_AVAILABLE(ios(5.0));

总结

/// 子控制器数组,不包括任何已经显示的控制器
@property(nonatomic,readonly) NSArray *childViewControllers;
/// 添加子控制器,如果子控制器拥有一个不同的父控制器,默认会先调用removeFromParentViewController将其移除- (void)addChildViewController:(UIViewController *)childController;
/// 从其父控制器的子控制器数组中将其删除- (void) removeFromParentViewController;
/// 子控制器转化函数- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion;
/// 添加子控制器后,该方法会自动调用- (void)willMoveToParentViewController:(UIViewController *)parent;
/// 删除子控制器后,该方法会自动调用- (void)didMoveToParentViewController:(UIViewController *)parent;

使用

  • 添加子控制器
 // 添加子控制器[superVC addChildViewController:childVC];// [childVC willMoveToParentViewController: superVC]; [addChildViewController:childVC]会自动调用,可以省略[superVC.view addSubview:childVC.view];[childVC didMoveToParentViewController:superVC];
  • 删除子控制器
 [childVC willMoveToParentViewController:nil];[childVC.view removeFromSuperview];[childVC removeFromParentViewController];// [childVC didMoveToParentViewController:nil]; [childVC removeFromParentViewController]会自动调用,可以省略
  • 切换子控制器的综合运用
 // 一开始显示的VC[self addChildViewController:self.allVC];[self.containerView addSubview:self.allVC.view];[self.allVC didMoveToParentViewController:self];// 保存数据self.oldVC = self.allVC;// 切换nowVC// 添加子控制器[self addChildViewController:self.nowVC];// 切换视图[self transitionFromViewController:self.oldVC toViewController:self.nowVC duration:0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) {if (finished) {[self.nowVC didMoveToParentViewController:self];[self.oldVC willMoveToParentViewController:nil];[self.oldVC removeFromParentViewController];// 数据保存self.oldVC = self.nowVC;}}];

参考资料

iOS使用addChildViewController
addChildViewController–控制器包控制器解耦

iOS添加子控制器(addChildViewController)相关推荐

  1. 子控制器 和 父控制器

    1.添加子控制器很简单 分两步 一个是 view 一个是 controller 1 [self addChildViewController:tableCTR]; 2 [self.view addSu ...

  2. addChildViewController时子控制器中的UI显示问题

    问题描述 在主控制器上添加一个子控制器,发现子控制器上的view元素的宽和高不是之前所给定的. 在ViewController里有一个childController,直接在- (void)viewDi ...

  3. 新浪微博开发-添加子视图控制器设置颜色

    一.添加子视图控制器 二.设置颜色 设置颜色:两种方法 一种较为繁琐,详见视频 第二种: //设置颜色 self.tabBar.tintColor = UIColor.orangeColor() 转载 ...

  4. iOS 添加在scrollview的子控件,用masonry布局的问题

    当在scrollview上添加子控件并用masonry布局时,发现运行后不是想要的布局,解决方法是需要添加一个view,上下左右,宽高.都要和scrollview一致.然后再在view上添加子控件即可 ...

  5. iOS开发者账号添加子账号

    硬性标准:账号类型必须是公司类型($99)或者是企业类型($299),其他账号无法添加子账号 步骤:1,      进入开发者页面,如果页面上显示有这个选项,则账号能添加子账号,否则无法添加子账号 2 ...

  6. 一劳永逸,iOS 网页视图控制器通用类封装

    原文链接:http://www.jianshu.com/p/553424763585 随着 H5 的发展,在 iOS 开发中,网页视图的使用率逐渐提升,为了增加代码封装度.减轻开发负担,因此通常会对网 ...

  7. iOS开发之控制器创建与加载(生命周期)

    1.如何创建一个控制器 控制器常见的创建方式有以下几种: (1)通过storyboard创建 (2)直接创建 MJViewController *mj = [[MJViewController all ...

  8. iOS中UINavigationController控制器使用详解

    一.概述 UINavigationController用来管理视图控制器,在多视图控制器中常用.它以栈的形式管理视图控制器,管理视图控制器个数理论上不受限制(实际受内存限制),push和pop方法来弹 ...

  9. ASP.NET MVC3 快速入门--第二节 添加一个控制器

    MVC的全称为model-view-controller(模型-视图-控制器).MVC是一种开发应用程序的模式,这个模式已经具有了很好的框架架构,并且十分容易维护.使用MVC开发出来的应用程序一般包括 ...

  10. 【翻译转载】【官方教程】Asp.Net MVC4入门指南(2):添加一个控制器

    2. 添加一个控制器 · 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-c ...

最新文章

  1. Pycharm上传Gitlab
  2. Python+Dash快速web应用开发——基础概念篇
  3. mysql percona server_MySQL Study之--Percona Server版本
  4. python查看函数参数_python函数参数
  5. SpringMVC的请求-文件上传-文件上传的原理
  6. C++——《算法分析》实验肆——单源最短路径问题
  7. 苹果Apple Music正式登陆索尼PS5
  8. 剑指offer面试题[30]-最小的k个数
  9. 几个清华和交大学霸的公众号,值得学习
  10. CentOS安装jdk
  11. ASCII对应码表(键值)
  12. 迁移学习笔记1:简明手册笔记
  13. 无线路由器实现网络接入
  14. 关于“应用程序正常初始化(0xc0150002)失败”问题的解决方案
  15. table.render 中 cols 属性 【【问题】】
  16. 如何使用Transformers和Tokenizers从头开始训练新的语言模型
  17. wps服务器无响应是什么原因,windows10系统运行wps无响应的解决方案
  18. SpringBoot(五)整合Mybatis-Plus
  19. sql server delete语句删除行
  20. 【工业大数据】工业大数据:构建制造型企业新型能力

热门文章

  1. 全国省市区java_Jsoup获取全国地区数据(省市县镇村)
  2. 俺的web课设大作业
  3. 爬取当当网评论(1)
  4. bt和wifi的共存
  5. matlab三次方程求根,如何用matlab求一元三次方程的最小正根?
  6. iphone捷径未能连接服务器,ios13无法安装第三方捷径怎么办 不允许不受信任的快捷指令解决方法...
  7. 163邮箱注册申请入口,申请163的邮箱账号
  8. 您觉得目前网页最小字体应该多大呢?
  9. java怎么做rfif上位机软件,最简单的【上位机软件】详解
  10. shping cloud搭建大觅网-之sping cloud初体验