iOS内存优化-addchildController

iOS内存优化_addChildViewController

   iOS的性能优化有很多方面,今天发现虽然很常用但忽视了一个iOS5出现的API,addChildViewController对性能的优化,简单来说就是一个ViewController上面有好多sub View,这些子view不常用但是盖在上面也会耗内存,应该改为ViewController上面加好多sub ViewController,然后不加载sub View,需要用到的时候使用下面这行代码来加载sub View
transitionFromViewController:toViewController:duration:options:animations:completion

那么具体流程是:

1.调用addChildViewController

firstViewController=[[FirstViewController alloc]
initWithNibName:@"FirstViewController" bundle:nil];

[self addChildViewController:firstViewController];

这个时候firstViewController的view是没有加载的,只会调用FirstViewController的willMoveToParentViewController

2.然后真正加载view的时候

[contentView addSubview:thirdViewController.view];
开始调用ViewController的viewDidLoad,viewWillAppear,viewDidAppear方法

3.事先addChildViewController 方法添加了secondViewController,没有加载secondViewController的view,加载的时候用transitionFromViewController从FirstViewController跳转

[self transitionFromViewController:currentViewController toViewController:secondViewController duration:1 options:UIViewAnimationOptionTransitionCurlDown animations:^{
            }  completion:^(BOOL finished) {
                if (finished) {
                     currentViewController=secondViewController;
                }else{
                     currentViewController=oldViewController;
                }
            }];

控制器的生命周期如下:

changeViewController[17248:4467101] -[SecondViewController viewDidLoad]

changeViewController[17248:4467101] -[FirstViewController viewWillDisappear:]

hangeViewController[17248:4467101] -[SecondViewController viewWillAppear:]

changeViewController[17248:4467101] -[SecondViewController viewDidAppear:]

changeViewController[17248:4467101] -[FirstViewController viewDidDisappear:]

这是一种性能优化的方式,特此记录

你可能感兴趣的:(iOS内存优化-addchildController)