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