什么是Container View Controller

什么是Container View Controller?苹果文档是这么描述的: app

 A container view controller contains content owned by other view controllers.

 

也就是说一个View Controller显示的某部份内容属于另外一个View Controller,那么这个View Controller就是一个Container,好比UIKit中的UINavigationController,UITabBarController。 spa

 

在iOS 5以前苹果是不容许出现自定义的Container的 ,也就是说你建立的一个View Controller的view不能包含另外一个View Controller的view,这对于逻辑复杂的界面来讲,不易于功能拆分。也许曾经你为了某个公用的显示逻辑,直接将某个View Controller的view添加到另外一个View Controller的view上,而后发现能够正常显示和使用,但实际上这种行为是很是危险的。 code

 

iOS 5.0 开始支持Custom Container View Controller,开放了用于构建自定义Container的接口。若是你想建立一个本身的Container,那么有一些概念还得弄清楚。Container的主要职责就是管理一个或多个Child View Controller的展现的生命周期,须要传递显示以及旋转相关的回调。 接口

 

其实显示或者旋转的回调的触发的源头来自于window,一个app首先有一个主window,初始化的时候须要给这个主window指定一个rootViewController,window会将显示相关的回调(viewWillAppear:, viewWillDisappear:, viewDidAppear:, or viewDidDisappear: )以及旋转相关的回调(willRotateToInterfaceOrientation:duration: ,willAnimateRotationToInterfaceOrientation:duration:, didRotateFromInterfaceOrientation:)传递给rootViewController。rootViewController须要再将这些callbacks的调用传递给它的Child View Controllers。 生命周期

 

一. 父子关系范式 ci

实现一个Custom Container View Controller并非一个简单的事情,主要分为两个阶段:父子关系的创建以及父子关系的解除。若是pVC将cVC的view添加为本身的subview,那么cVC必须为pVC的Child View Controller,而反过来则不必定成立,好比UINavigationController,一个View Controller被push进来后便和navigationController创建父子关系了,可是只有最上面的View Controller 是显示着的,底下的View Controller的view则被移出了容器的view的显示层级,当一个View Controller被pop以后,便和navigationController解除了父子关系了。 rem

 

展现一个名为content的child view controller: 文档

 
  1. [self addChildViewController:content];  //1 
  2. content.view.frame = [self frameForContentController];  
  3. [self.view addSubview:self.currentClientView]; //2 
  4. [content didMoveToParentViewController:self]; //3 

1.将content添加为child view controller,addChildViewController:接口创建了逻辑上的父子关系,子能够经过parentViewController,访问其父VC,addChildViewController:接口的逻辑中会自动调用 [content willMoveToParentViewController:self]; it

2.创建父子关系后,即是将content的view加入到父VC的view hierarchy上,同时要决定的是 content的view显示的区域范围。  io

3.调用child的 didMoveToParentViewController: ,以通知child,完成了父子关系的创建

 

移除一个child view controller:

 
  1. [content willMoveToParentViewController:nil]; //1 
  2. [content.view removeFromSuperview]; //2 
  3. [content removeFromParentViewController]; //3 

1.通知child,即将解除父子关系,从语义上也能够看出 child的parent即将为nil

2.将child的view从父VC的view的hierarchy中移除 

3.经过removeFromParentViewController的调用真正的解除关系,removeFromParentViewController会自动调用 [content didMoveToParentViewController:nil]

相关文章
相关标签/搜索