View Controller Containment

2016-07-16  本文已影响83人  我是花老虎
  1. willMove(toParentViewController:)
  1. didMove(toParentViewController:)
  1. 把子vc的view加到父vc的view上
    [self addChildViewController:_startMapViewController];          //  1  作为子VC,会自动调用子VC的willMoveToParentViewController方法。
    [topContainer addSubview:_startMapViewController.view];         //  2 子VC的view被作为subview添加到第一个viewContainer中
    [_startMapViewController didMoveToParentViewController:self];   //  3 子VC被通知拥有一个父VC。
  1. 把子VC的view移除
[fromController willMoveToParentViewController:nil];//
调用转场方法或者[fromController.view removeFromSuperView]
[fromController removeFromParentViewController];    // 

5 transition(from:to:duration:options:animations:completion:)

This method adds the second view controller'��s view to the view hierarchy and then performs the animations defined in your animations block. After the animation completes, it removes the first view controller'��s view from the view hierarchy.

这个函数首先把第二个VC的view加到父vc的字view上,然后执行动画,最后把第一个vc的view从view hierarchy中移除。

{
    toController.view.frame = fromController.view.bounds;                           //  1
    [self addChildViewController:toController];                                     //  
    [fromController willMoveToParentViewController:nil];                            //  

    [self transitionFromViewController:fromController
                      toViewController:toController
                              duration:0.2
                               options:direction | UIViewAnimationOptionCurveEaseIn
                            animations:nil
                            completion:^(BOOL finished) {

                                [toController didMoveToParentViewController:self];  //  2
                                [fromController removeFromParentViewController];    //  3
                            }];
}

这个函数是把fromVC及其View移除,toVC及其View加到界面上去。

    [fromController willMoveToParentViewController:nil]; //函数调用前
    [fromController removeFromParentViewController];    //  动画block中
    [fromController.view removeFromSuperView] //动画block之后
    [self addChildViewController:toController]; //函数调用前
    [self.view addSubView:toController.view] //动画block之前       
    [toController didMoveToParentViewController:self];  //  block之中

View Controller Containment

上一篇下一篇

猜你喜欢

热点阅读