Addchildviewcontroller的简单理解和使用

转载请注明出处!!!     

  在APP中经常有一个导航栏下面有很多类型的界面。如果只是数据不同,界面大体相同很简单,刷新就可。但是如果界面不一致,就会有些麻烦。这时候用addchildviewcontroller就可以了。

     苹果新的API增加了addChildViewController方法,并且希望我们在使用addSubview时,同时调用[self addChildViewController:child]方法将sub view对应的viewController也加到当前ViewController的管理中。

对于那些当前暂时不需要显示的subview,只通过addChildViewController把subViewController加进去;需要显示时再调用transitionFromViewController方法。将其添加进入底层的ViewController中。

     这样做的好处:

     1.无疑,对页面中的逻辑更加分明了。相应的View对应相应的ViewController。

     2.当某个子View没有显示时,将不会被Load,减少了内存的使用。

     3.当内存紧张时,没有Load的View将被首先释放,优化了程序的内存释放机制。

直接上代码:

- (void)viewDidLoad {

    [super viewDidLoad];    

    self.title = @"仿网易导航栏";

    self.headArray = @[@"11",@"22",@"33"];

    [self addHeadView];

    [self addChildVC];

 }

- (void)addHeadView {

    // ? 不加文字scrollview 文字不显示

    self.automaticallyAdjustsScrollViewInsets = NO;

    

    self.headScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64,SCREEN_WIDTH, 40)];

    self.headScrollView.backgroundColor = [UIColor purpleColor];

    self.headScrollView.showsVerticalScrollIndicator = NO;

    self.headScrollView.contentSize = CGSizeMake(560, 0);

    self.headScrollView.bounces = NO;

    self.headScrollView.pagingEnabled = YES;

    [self.view addSubview:self.headScrollView];

    for (int i = 0; i < [self.headArray count]; i++) {

        

        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

        button.frame = CGRectMake(0 + i*80, 0, 80, 40);

        [button setTitle:[self.headArray objectAtIndex:i] forState:UIControlStateNormal];

        button.tag = i + 100;

        [button addTarget:self action:@selector(didClickHeadButtonAction:) forControlEvents:UIControlEventTouchUpInside];

        [self.headScrollView addSubview:button];

    }

}

 

- (void)addChildVC {

    self.firstVC = [[ChildViewController alloc] init];

    self.firstVC.view.backgroundColor = [UIColor redColor];

    [self.firstVC.view setFrame:CGRectMake(0, 104, SCREEN_WIDTH, SCREEN_HEIGHT-104)];

    [self addChildViewController:_firstVC];

    //addChildViewController 会调用 [child willMoveToParentViewController:self] 方法,但是不会调用 didMoveToParentViewController:方法,官方建议显示调用

    [self.firstVC didMoveToParentViewController:self];

    //  默认,第一个视图(你会发现,全程就这一个用了addSubview)

    [self.view addSubview:self.firstVC.view];

    self.currentVC = self.firstVC;

   

    self.secondVC = [[ChildViewController alloc] init];

    self.secondVC.view.backgroundColor = [UIColor greenColor];

    [self.secondVC.view setFrame:CGRectMake(0, 104, SCREEN_WIDTH, SCREEN_HEIGHT-104)];

    

    self.thirdVC = [[ChildViewController alloc] init];

    self.thirdVC.view.backgroundColor = [UIColor blueColor];

    [self.thirdVC.view setFrame:CGRectMake(0, 104, SCREEN_WIDTH, SCREEN_HEIGHT-104)];

}

 

- (void)didClickHeadButtonAction:(UIButton *)button {

    //  展示2个,其余一样,自行补全噢

    switch (button.tag) {

        case 100:

            [self replaceController:self.currentVC newController:self.firstVC];

            break;

        case 101:

            [self replaceController:self.currentVC newController:self.secondVC];

            break;

        case 102:

            [self replaceController:self.currentVC newController:self.thirdVC];

            break;

        default:

            break;

    }

}

 

//  切换各个标签内容

- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController {

    /**

     *            着重介绍一下它

     *  transitionFromViewController:toViewController:duration:options:animations:completion:

     *  fromViewController      当前显示在父视图控制器中的子视图控制器

     *  toViewController        将要显示的姿势图控制器

     *  duration                动画时间(这个属性,old friend 了 O(∩_∩)O)

     *  options                 动画效果(渐变,从下往上等等,具体查看API)

     *  animations              转换过程中得动画

     *  completion              转换完成

     */        

    [self addChildViewController:newController];

    [self transitionFromViewController:oldController toViewController:newController duration:2.0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:^(BOOL finished) {

        if (finished) {

            // 移除oldController,但在removeFromParentViewController:方法前不会调用willMoveToParentViewController:nil 方法,所以需要显示调用

            [newController didMoveToParentViewController:self];

            [oldController willMoveToParentViewController:nil];

            [oldController removeFromParentViewController];

            self.currentVC = newController;

        }else{

            self.currentVC = oldController;

            }

    }];

}

   // 上面的代码实现后效果类似于网易新闻界面。但是也有一个界面上有两种以上的类型页面。这时也可以用这个实现。

- (void)addChildVC {

  UIViewController FirstVC * first = [[FirstVC alloc] init];
  [self addChildViewController:first];
  //addChildViewController 会调用 [child willMoveToParentViewController:self] 方法,但是不会调用 didMoveToParentViewController:方法,官方建议显示调用
  [first didMoveToParentViewController:self];
  [first.view setFrame:CGRectMake(0, CGRectGetMaxY(myScrollView.frame), width, 300)];
  [self.view addSubview:first.view];
  SecondVC * second = [[SecondVC alloc] init];
  [self addChildViewController:second];
  [second didMoveToParentViewController:self];
  [second.view setFrame:CGRectMake(0,CGRectGetMaxY(first.view.frame), width, 300)];
  [self.view addSubview:second.view];
}
这就实现了上下两个子界面了。
 
补充:需要注意的地方
1. 容器controller最好定义一个专门用来展示子controller相关view的区域。我的代码中没有,毕竟这只是一个Demo。而且如果你定义了一个view后,masksToBounds很重要,要不然,整个controller都会被展示出来的.
2. 调用完addChildViewController之后还需要调用didMoveToParentViewController
原文地址:https://www.cnblogs.com/weicyNo-1/p/7299142.html