UIViewController增加SubController

2016-11-23  本文已影响0人  C_冠文

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:<MainViewController: 0x79ea92e0>
FirstViewController.m:73      -[FirstViewController didMoveToParentViewController:]
SecondViewController.m:68     -[SecondViewController willMoveToParentViewController:] vc:<MainViewController: 0x79ea92e0>
SecondViewController.m:72     -[SecondViewController didMoveToParentViewController:] vc:<MainViewController: 0x79ea92e0>
ThirdViewController.m:62      -[ThirdViewController willMoveToParentViewController:] vc:<MainViewController: 0x79ea92e0>
ThirdViewController.m:66      -[ThirdViewController didMoveToParentViewController:] vc:<MainViewController: 0x79ea92e0>
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:<MainViewController: 0x79ea92e0>
SecondViewController.m:72     -[SecondViewController didMoveToParentViewController:] vc:<MainViewController: 0x79ea92e0>

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)

4、切换

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

5、UIStoryBoard创建(xib没有)

8758DC4C-501B-4AA9-BE32-C445F72A6D49.png
上一篇下一篇

猜你喜欢

热点阅读