UIViewController增加SubController

iOS5后,ViewController可以像navigationVC,tabbarVC一样,增加、删除、切换subController

[TOC]


注意

  1. 当我们调用addChildViewController方法后,必须调用didMoveToParentViewController方法。
    [子视图控制器 didMoveToParentViewController:父视图控制器]
  2. 当调用removeFromParentViewController方法前,必须先调用willMoveToParentViewController方法,且parent参数为nil:
    [将要删除的视图控制器 willMoveToParentViewController:nil];
  3. 当从一个视图控制容器中添加或者移除viewController后,该方法被调用。
    -(void)willMoveToParentViewController:(UIViewController *)parent -(void)didMoveToParentViewController:(UIViewController *)parent parent:父视图控制器,添加时parent为父视图控制器,删除时parent为nil
 MainViewController.m:51      添加
FirstViewController.m:68      -[FirstViewController willMoveToParentViewController:] vc:
FirstViewController.m:73      -[FirstViewController didMoveToParentViewController:]
SecondViewController.m:68     -[SecondViewController willMoveToParentViewController:] vc:
SecondViewController.m:72     -[SecondViewController didMoveToParentViewController:] vc:
ThirdViewController.m:62      -[ThirdViewController willMoveToParentViewController:] vc:
ThirdViewController.m:66      -[ThirdViewController didMoveToParentViewController:] vc:
MainViewController.m:64   第3VC先显示
ThirdViewController.m:32      -[ThirdViewController viewDidLoad]
ThirdViewController.m:43      -[ThirdViewController viewWillAppear:]
ThirdViewController.m:48      -[ThirdViewController viewDidAppear:]
MainViewController.m:82   切换
MainViewController.m:103      生日提醒
SecondViewController.m:36     -[SecondViewController viewDidLoad]
ThirdViewController.m:53      -[ThirdViewController viewWillDisappear:]
SecondViewController.m:49     -[SecondViewController viewWillAppear:]
SecondViewController.m:54     -[SecondViewController viewDidAppear:]
ThirdViewController.m:58      -[ThirdViewController viewDidDisappear:]
MainViewController.m:71   删除
SecondViewController.m:68     -[SecondViewController willMoveToParentViewController:] vc:(null)
SecondViewController.m:59     -[SecondViewController viewWillDisappear:]
SecondViewController.m:72     -[SecondViewController didMoveToParentViewController:] vc:(null)
SecondViewController.m:64     -[SecondViewController viewDidDisappear:]

1、 addChildViewController的由来

在苹果的WWDC2011大会视频的 《Session 101 - What’s New in Cocoa》 和 《Session 102 - Implementing UIViewController Containment》 中介绍了苹果在iOS5中给UIViewController新增加的5方法以及一个属性:

// 方法
addChildViewController:
removeFromParentViewController:
transitionFromViewController:toViewController:duration:options:animations:completion:
willMoveToParentViewController:
didMoveToParentViewController:

// 属性
@property(nonatomic,readonly) NSArray *childViewControllers

2、添加(会常用到)

#pragma mark - 添加子控制器
- (void)addContentController:(UIViewController*)newVC
{
    [self addChildViewController:newVC];
    [self.view addSubview:newVC.view];
    [newVC didMoveToParentViewController:self];//必需调用这句 willMove:self由系统调用
}

打印

SecondViewController.m:68     -[SecondViewController willMoveToParentViewController:] vc:
SecondViewController.m:72     -[SecondViewController didMoveToParentViewController:] vc:
  • 在调用addChildViewController之后,必需调用didMoveToParentViewController:parentViewController:parentVC(若不调用该方法,chileVC中的didMoveToParent方法不会自动调用)。willMoveToParentViewController默认调用了。

3、删除

#pragma mark - 删除子控制器
- (void)deleteContentController: (UIViewController*)viewController {
    [viewController willMoveToParentViewController:nil];//删除前必需调用,系统默认调用didMove:nil
    [viewController.view removeFromSuperview];
    [viewController removeFromParentViewController];
}

打印

SecondViewController.m:68     -[SecondViewController willMoveToParentViewController:] vc:(null)
SecondViewController.m:72     -[SecondViewController didMoveToParentViewController:] vc:(null)
  • 删除时,需要将View先删除
  • 在调用[子视图控制器 removeFromParentViewController]之前,必须先调用[子视图控制器 willMoveToParentViewController:nil]方法,系统默认调用。

4、切换

#pragma mark - 切换子控制器(切换)
- (void)changeFromViewController: (UIViewController*)oldVC
                toViewController: (UIViewController*)newVC {
    
    [self transitionFromViewController: oldVC toViewController: newVC
                              duration: 0.25 options:0
                            animations: Nil
                            completion: Nil];
}
  • 切换只在2个chileController进行

5、UIStoryBoard创建(xib没有)

  • 在StoryBoard中拖UIContainerView并绑定Controller,其效果与2使用代码添加效果是一样的
UIViewController增加SubController_第1张图片
8758DC4C-501B-4AA9-BE32-C445F72A6D49.png

你可能感兴趣的:(UIViewController增加SubController)