Addchildviewcontroller的簡單理解和使用

在APP中經常有一個導航欄下面有很多類型的界面。如果只是數據不同,界面大體相同很簡單,刷新就可。但是如果界面不一致,就會有些麻煩。這時候用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將被首先釋放,優化了程序的內存釋放機制。

直接上代碼:

- (void)viewDidLoad {

[super viewDidLoad];

self.title=@"仿網易導航欄";

self.headArray= @[@"11",@"22",@"33"];

[self addHeadView];

[self addChildVC];

}- (void)addHeadView {//? 不加文字scrollview 文字不顯示self.automaticallyAdjustsScrollViewInsets=NO;

self.headScrollView= [[UIScrollView alloc] initWithFrame:CGRectMake(0,64,SCREEN_WIDTH,40)];

self.headScrollView.backgroundColor=[UIColor purpleColor];

self.headScrollView.showsVerticalScrollIndicator=NO;

self.headScrollView.contentSize= CGSizeMake(560,0);

self.headScrollView.bounces=NO;

self.headScrollView.pagingEnabled=YES;

[self.view addSubview:self.headScrollView];for(inti =0; i < [self.headArray count]; i++) {

UIButton*button =[UIButton buttonWithType:UIButtonTypeSystem];

button.frame= CGRectMake(0+ i*80,0,80,40);

[button setTitle:[self.headArray objectAtIndex:i] forState:UIControlStateNormal];

button.tag= i +100;

[button addTarget:self action:@selector(didClickHeadButtonAction:) forControlEvents:UIControlEventTouchUpInside];

[self.headScrollView addSubview:button];

}

}- (void)addChildVC {

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

self.firstVC.view.backgroundColor=[UIColor redColor];

[self.firstVC.view setFrame:CGRectMake(0,104, SCREEN_WIDTH, SCREEN_HEIGHT-104)];

[self addChildViewController:_firstVC];//addChildViewController 會調用 [child willMoveToParentViewController:self] 方法,但是不會調用 didMoveToParentViewController:方法,官方建議顯示調用[self.firstVC didMoveToParentViewController:self];//默認,第一個視圖(你會發現,全程就這一個用了addSubview)[self.view addSubview:self.firstVC.view];

self.currentVC=self.firstVC;

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

self.secondVC.view.backgroundColor=[UIColor greenColor];

[self.secondVC.view setFrame:CGRectMake(0,104, SCREEN_WIDTH, SCREEN_HEIGHT-104)];

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

self.thirdVC.view.backgroundColor=[UIColor blueColor];

[self.thirdVC.view setFrame:CGRectMake(0,104, SCREEN_WIDTH, SCREEN_HEIGHT-104)];

}- (void)didClickHeadButtonAction:(UIButton *)button {//展示2個,其余一樣,自行補全噢switch(button.tag) {case100:

[self replaceController:self.currentVC newController:self.firstVC];break;case101:

[self replaceController:self.currentVC newController:self.secondVC];break;case102:

[self replaceController:self.currentVC newController:self.thirdVC];break;default:break;

}

}//切換各個標簽內容- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController {/**

*? ? ? ? ? ? 著重介紹一下它

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

*? fromViewController? ? ? 當前顯示在父視圖控制器中的子視圖控制器

*? toViewController? ? ? ? 將要顯示的姿勢圖控制器

*? duration? ? ? ? ? ? ? ? 動畫時間(這個屬性,old friend 了 O(∩_∩)O)

*? options? ? ? ? ? ? ? ? 動畫效果(漸變,從下往上等等,具體查看API)

*? animations? ? ? ? ? ? ? 轉換過程中得動畫

*? completion? ? ? ? ? ? ? 轉換完成*/[self addChildViewController:newController];

[self transitionFromViewController:oldController toViewController:newController duration:2.0options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:^(BOOL finished) {if(finished) {//移除oldController,但在removeFromParentViewController:方法前不會調用willMoveToParentViewController:nil 方法,所以需要顯示調用[newController didMoveToParentViewController:self];

[oldController willMoveToParentViewController:nil];

[oldController removeFromParentViewController];

self.currentVC=newController;

}else{

self.currentVC=oldController;

}

}];

}//上面的代碼實現后效果類似于網易新聞界面。但是也有一個界面上有兩種以上的類型頁面。這時也可以用這個實現。- (void)addChildVC {

UIViewController FirstVC* first =[[FirstVC alloc] init];

[self addChildViewController:first];//addChildViewController 會調用 [child willMoveToParentViewController:self] 方法,但是不會調用 didMoveToParentViewController:方法,官方建議顯示調用[first didMoveToParentViewController:self];

[first.view setFrame:CGRectMake(0, CGRectGetMaxY(myScrollView.frame), width,300)];

[self.view addSubview:first.view];

SecondVC* second =[[SecondVC alloc] init];

[self addChildViewController:second];

[second didMoveToParentViewController:self];

[second.view setFrame:CGRectMake(0,CGRectGetMaxY(first.view.frame), width,300)];

[self.view addSubview:second.view];

}

這就實現了上下兩個子界面了。

補充:需要注意的地方

1. 容器controller最好定義一個專門用來展示子controller相關view的區域。我的代碼中沒有,畢竟這只是一個Demo。而且如果你定義了一個view后,masksToBounds很重要,要不然,整個controller都會被展示出來的.

2. 調用完addChildViewController之后還需要調用didMoveToParentViewController

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容