蘋果新的API增加了addChildViewController方法衬鱼,并且希望我們在使用addSubview時(shí)搔啊,同時(shí)調(diào)用[self addChildViewController:child]方法將sub view對應(yīng)的viewController也加到當(dāng)前ViewController的管理中。
? ? 對于那些當(dāng)前暫時(shí)不需要顯示的subview蘸拔,只通過addChildViewController把subViewController加進(jìn)去师郑;需要顯示時(shí)再調(diào)用transitionFromViewController方法。將其添加進(jìn)入底層的ViewController中调窍。
這樣做的好處:
1.無疑宝冕,對頁面中的邏輯更加分明了。相應(yīng)的View對應(yīng)相應(yīng)的ViewController邓萨。
2.當(dāng)某個(gè)子View沒有顯示時(shí)地梨,將不會(huì)被Load,減少了內(nèi)存的使用缔恳。
3.當(dāng)內(nèi)存緊張時(shí)宝剖,沒有Load的View將被首先釋放,優(yōu)化了程序的內(nèi)存釋放機(jī)制褐耳。
*/
/**
* ?在iOS5中诈闺,ViewController中新添加了下面幾個(gè)方法:
* ?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)];
// ?默認(rèn),第一個(gè)視圖(你會(huì)發(fā)現(xiàn),全程就這一個(gè)用了addSubview)
[self.view addSubview:self.firstVC.view];
self.currentVC = self.firstVC; ?