[IOS筆記] - addChildViewController

View Controller中能夠添加多個sub view,在須要的時候顯示出來;spa

能夠經過viewController(parent)中能夠添加多個child viewController;來控制頁面中的sub view,下降代碼耦合度;code

經過切換,能夠顯示不一樣的view;,替代以前的addSubView的管理blog

 

   //在parent view controller 中添加 child view controller
   FirstViewController *firstViewController=[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    [self addChildViewController:firstViewController];
    
    SecondViewController *secondViewController=[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [self addChildViewController:secondViewController];
    
    ThirdViewController *thirdViewController=[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    [self addChildViewController:thirdViewController];
    
    [self.view  addSubview:thirdViewController.view];
  
// addChildViewController回調用[child willMoveToParentViewController:self] ,可是不會調用didMoveToParentViewController,因此須要顯示調用 [thirdViewController didMoveToParentViewController:self]; currentViewController=thirdViewController;   //切換child view controller [self transitionFromViewController:currentViewController toViewController:firstViewController duration:4 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{ } completion:^(BOOL finished) { //...... }]; currentViewController=firstViewController;   //移除child view controller // removeFromParentViewController在移除child前不會調用[self willMoveToParentViewController:nil] ,因此須要顯示調用 [currentViewController willMoveToParentViewController:nil]; [currentViewController removeFromSuperview]; [currentViewController removeFromParentViewController];
相關文章
相關標籤/搜索