OS 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将被首先释放,优化了程序的内存释放机制。

*/

/**

*  在iOS5中,ViewController中新添加了下面几个方法:

*  addChildViewController:

*  removeFromParentViewController

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

*  willMoveToParentViewController:

*  didMoveToParentViewController:

   */

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

[self.firstVC.view setFrame:CGRectMake(0, 104, 320, 464)];

[self addChildViewController:_firstVC];

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

[self.secondVC.view setFrame:CGRectMake(0, 104, 320, 464)];

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

[self.thirdVC.view setFrame:CGRectMake(0, 104, 320, 464)];

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

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

self.currentVC = self.firstVC;  

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容