1.简介
Container的主要职责就是管理一个或多个Child View Controller的展示的生命周期,需要传递显示以及旋转相关的回调
一个app首先有一个主window,window指定一个rootViewController
window会将相关的回调传递给rootViewController
rootViewController需要再将这些callbacks的调用传递给它的Child View Controllers
显示相关的回调:appearance callbacks( viewWillAppear: viewWillDisappear: viewDidAppear: viewDidDisappear: )旋转相关的回调:rotation callbacks( willRotateToInterfaceOrientation:duration: willAnimateRotationToInterfaceOrientation:duration:, didRotateFromInterfaceOrientation:)
2.父子关系(parentViewController--subViewController)
实现一个Custom Container View Controller分为两个阶段
①父子关系的建立②️父子关系的解除
2.1建立父子关系
//将subViewController添加为child view controller,addChildViewController:接口建立了逻辑上的父子关系 //子可以通过parentViewController,访问其父VC //addChildViewController:接口的逻辑中会自动调用 [subViewController willMoveToParentViewController : parentViewController]; [parentViewController addChildViewController : subViewController]; //建立父子关系后,便是将content的view加入到父VC的view hierarchy(层次结构)上 [parentViewController.view addSubview : subViewController.view]; //调用child的 didMoveToParentViewController: ,以通知child,完成了父子关系的建立 [subViewController didMoveToParentViewController : parentViewController];
2.2解除父子关系
//通知child,即将解除父子关系 [self willMoveToParentViewController:nil]; //将child的view从父VC的view的hierarchy中移除 [self.view removeFromSuperview]; //通过removeFromParentViewController的调用真正的解除关系 //removeFromParentViewController会自动调用 [content didMoveToParentViewController:nil] [self removeFromParentViewController];
3. appearance callbacks的传递
生命周期:
alloc -> initWithNibName -> loadView -> viewDidLoad -> viewWillAppear -> viewDidAppear -> viewWillDisappear -> viewDidDisappear -> dealloc
其实,callbacks系统会自动调用,但是有些需求自动调用不能满足需求,如:
child view的展示有一个动画的过程,这个时候我们并不想viewDidAppear的调用在addSubview的时候进行,而是等展示动画结束后再调用viewDidAppear。
自定义容器中需要关闭自动调用callbacks,自己去控制callbacks的调用时机
3.1如何关闭自动传递:
IOS5: automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers return NO; IOS6+:shouldAutomaticallyForwardAppearanceMethods return NO;
3.2手动传递
手动传递需要用到一下两个方法, 而且必须成对出现
beginAppearanceTransition:animated:endAppearanceTransition
//触发subViewController的viewWillAppear [subViewController beginAppearanceTransition:YES animated:animated]; //触发subViewController的viewWillDisappear [subViewController beginAppearanceTransition:NO animated:animated]; //触发对应的viewDidAppear和viewDidDisappear [subViewController endAppearanceTransition];
例子
/***将subViewController添加入parentViewController中*调用该方法后,可以添加子控制器显示时的动画效果*/-(void)addToParentViewContoller:(UIViewController *)parentViewController subViewController:(UIViewController*)subViewController{ [subViewController beginAppearanceTransition:YES animated:animated]; [parentViewController addChildViewController : subViewController]; [parentViewController.view addSubview : subViewController.view]; [subViewController didMoveToParentViewController : parentViewController]; [subViewController endAppearanceTransition]; }/*** 将subViewController从parentViewController中删除*/-(void)removeFromParentViewContoller:(UIViewController *)parentViewController subViewController:(UIViewController*)subViewController{ [subViewController beginAppearanceTransition:NO animated:animated]; [subViewController willMoveToParentViewController:nil]; [subViewController.view removeFromSuperview]; [subViewController removeFromParentViewController]; [subViewController endAppearanceTransition]; }
4. rotation callbacks的传递
转屏时才会需要关注的问题
4.1如何关闭自动传递:
IOS5: automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers return NO; IOS6+:shouldAutomaticallyForwardRotationMethods return NO;
转屏时触发的函数:请参照如下链接:
更多详细内容请参照如下链接: