自定义 Container View Controller

自定义 Container View Controller

定义:在一个 View Controller 中包含多个 View Controller 时,那么这个 View Controller 就是一个 Container View Controller

作用:通过 Container 来对 View Controller 上的 controllers 的生命周期进行管理以及旋转相关的处理。

如何在视图控制器中添加子类视图控制器

在 iOS5 之后,可以通过 addChildViewController 来进行 childViewController 的添加,这样也就添加了一个 parentViewController,形成了一个父子关系。下面看下官方文档对于 addChildViewController 的介绍:

/*
  If the child controller has a different parent controller, it will first be removed from its current parent
  by calling removeFromParentViewController. If this method is overridden then the super implementation must
  be called.
*/

- (void)addChildViewController:(UIViewController *)childController NS_AVAILABLE_IOS(5_0);

它是这样描述的,当我们有一个不同的 parent controller 我们首先调用 removeFromParentViewController 来删除原来的 parent controller。

所以,我们是第一次使用,不需要删除之前的父视图控制器,记得在添加之后需要添加其父视图控制器,对其子类生命周期、旋转特性进行管理

[self addChildViewController:content]; 
[self.view addSubview:self.currentClientView];  
[content didMoveToParentViewController:self];

如何在视图控制器中删除子类视图控制器

移除子视图控制器则需要在 removeFromSuperview 的同时 removeFromParentViewController

[content willMoveToParentViewController:nil]; // 删除前通知
[content.view removeFromSuperview]; 
[content removeFromParentViewController];

如何处理子类视图控制器生命周期

通过以上的形式可以正常的添加和移除视图控制器了,但是如何进行生命周期的管理呢?由于 addSubview 之后,会自动调用 viewDidAppear 等方法,这些是系统自动调用的,在 UIViewController 中,有如下方法可以自动调用 viewDidAppear等方法

// If a custom container controller manually forwards its appearance callbacks, then rather than calling
// viewWillAppear:, viewDidAppear: viewWillDisappear:, or viewDidDisappear: on the children these methods
// should be used instead. This will ensure that descendent child controllers appearance methods will be
// invoked. It also enables more complex custom transitions to be implemented since the appearance callbacks are
// now tied to the final matching invocation of endAppearanceTransition.
- (void)beginAppearanceTransition:(BOOL)isAppearing animated:(BOOL)animated __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
- (void)endAppearanceTransition __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);

/*
  This method is consulted to determine if a view controller manually forwards its containment callbacks to
  any children view controllers. Subclasses of UIViewController that implement containment logic may override
  this method. The default implementation returns YES. If it is overridden and returns NO, then the subclass is
  responsible for forwarding the following methods as appropriate - viewWillAppear: viewDidAppear: viewWillDisappear:
  viewDidDisappear: willRotateToInterfaceOrientation:duration:
  willAnimateRotationToInterfaceOrientation:duration: didRotateFromInterfaceOrientation:
*/

-(BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers NS_DEPRECATED_IOS(5_0,6_0) __TVOS_PROHIBITED;
- (BOOL)shouldAutomaticallyForwardRotationMethods NS_DEPRECATED_IOS(6_0,8_0, "Manually forward viewWillTransitionToSize:withTransitionCoordinator: if necessary") __TVOS_PROHIBITED;

- (BOOL)shouldAutomaticallyForwardAppearanceMethods NS_AVAILABLE_IOS(6_0);  

上面方法的意思就是说,通过在父视图控制器中将 automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers 为 false , 那么系统不会自动管理子视图控制器的生命周期,使用 beginAppearanceTransition:(BOOL)isAppearing animated:(BOOL)animated 和 endAppearanceTransition 来处理。

当 isAppearing = YES 处理 willAppear 情况,endAppearanceTransition 来处理viewDidAppear。

当 isAppearing = NO 时,处理 willDissAppear, endAppearanceTransition 来处理viewDidDissAppear.

参考资料:Custom Container View Controller

下一篇,通过一个例子来进行实战

你可能感兴趣的:(自定义 Container View Controller)