單獨使用 addSubview 去加viewController的view發現有許多問題,主要是使用了代理方法的。查了下發現問題所在。 蘋果新的API增加了addChildViewController方法,并且希望我們在使用addSubview時,同時調用[self addChildViewController:child]方法將sub view對應的viewController也加到當前ViewController的管理中。 對于那些當前暫時不需要顯示的subview,只通過addChildViewController把subViewController加進去;需要顯示時再調用transitionFromViewController方法。將其添加進入底層的ViewController中。
//點擊切換頁面 - (void)didClick:(UIButton *)button { // 點擊處于當前頁面的按鈕,直接跳出 其他頁面 [self replaceController:self.currentVC newController:self.firstVC];
}
// 切換各個標簽內容 - (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.0 options:UIViewAnimationOptionTransitionCrossDissolve 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; } }];}
新聞熱點
疑難解答