標籤:ios開發 childviewcontroller parentviewcontroller 視圖切換
ChildViewController的應用
viewControlle中可以添加多個subView,在需要的時候顯示出來;另一種方法是通過向parentViewController中可以添加多個childCiewController;來控制頁面中的subView,降低代碼耦合度;通過切換子視圖控制器,可以顯示不同的view;,替代之前的addSubView的管理。
本節通過類似百度新聞模組切換的介面來示範ChileViewController的應用:
文檔結構:
代碼示範:
#import "MainViewController.h"#import "FirstViewController.h"#import "SecondViewController.h"#import "ThirdViewController.h"@interface MainViewController ()@property (nonatomic, strong) FirstViewController *firstVC;@property (nonatomic, strong) SecondViewController *secondVC;@property (nonatomic, strong) ThirdViewController *thirdVC;@property (nonatomic, strong) UIViewController *currentVC; @property (nonatomic, strong) UIScrollView *headScrollView;@property (nonatomic, strong) NSMutableArray *itemArray;@property (nonatomic, strong) UIView *contentView;@end @implementation MainViewController- (void)loadView{ [super loadView]; [self initialization];} - (void)viewDidLoad { [super viewDidLoad]; [self loadBaseUI];} - (void)initialization{ _itemArray = [NSMutableArray arrayWithObjects:@"頭條",@"今日",@"焦點", nil];} - (void)loadBaseUI{ self.title = @"首頁"; _headScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)]; _headScrollView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000]; for (int i = 0; i<_itemArray.count; i++) { UIButton *itemButton = [[UIButton alloc]initWithFrame:CGRectMake(i*([UIScreen mainScreen].bounds.size.width/_itemArray.count), 0, [UIScreen mainScreen].bounds.size.width/_itemArray.count, 44)]; itemButton.tag = 100+i; itemButton.backgroundColor = [UIColor clearColor]; NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor purpleColor],NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}; [itemButton setAttributedTitle:[[NSAttributedString alloc]initWithString:_itemArray[i] attributes:dic] forState:UIControlStateNormal]; [itemButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; [_headScrollView addSubview:itemButton]; } [_headScrollView setContentSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 44)]; _headScrollView.showsHorizontalScrollIndicator = NO; _headScrollView.showsVerticalScrollIndicator = NO; [self.view addSubview:_headScrollView]; _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 44, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 44 - 64)]; _contentView.backgroundColor = [UIColor clearColor]; [self.view addSubview:_contentView]; [self addSubControllers];} #pragma mark - privatemethods- (void)addSubControllers{ _firstVC = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil]; [self addChildViewController:_firstVC]; _secondVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; [self addChildViewController:_secondVC]; _thirdVC = [[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil]; [self addChildViewController:_thirdVC]; //調整子視圖控制器的Frame已適應容器View [self fitFrameForChildViewController:_firstVC]; //設定預設顯示在容器View的內容 [self.contentView addSubview:_firstVC.view]; NSLog(@"%@",NSStringFromCGRect(self.contentView.frame)); NSLog(@"%@",NSStringFromCGRect(_firstVC.view.frame)); _currentVC = _firstVC;} - (void)buttonClick:(UIButton *)sender{ if ((sender.tag == 100 && _currentVC == _firstVC) || (sender.tag == 101 && _currentVC == _secondVC) || (sender.tag == 102 && _currentVC == _thirdVC)) { return; } switch (sender.tag) { case 100:{ [self fitFrameForChildViewController:_firstVC]; [self transitionFromOldViewController:_currentVC toNewViewController:_firstVC]; } break; case 101:{ [self fitFrameForChildViewController:_secondVC]; [self transitionFromOldViewController:_currentVC toNewViewController:_secondVC]; } break; case 102:{ [self fitFrameForChildViewController:_thirdVC]; [self transitionFromOldViewController:_currentVC toNewViewController:_thirdVC]; } break; }} - (void)fitFrameForChildViewController:(UIViewController *)chileViewController{ CGRect frame = self.contentView.frame; frame.origin.y = 0; chileViewController.view.frame = frame;} //轉換子視圖控制器- (void)transitionFromOldViewController:(UIViewController *)oldViewControllertoNewViewController:(UIViewController *)newViewController{ [self transitionFromViewController:oldViewController toViewController:newViewController duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) { if (finished) { [newViewController didMoveToParentViewController:self]; _currentVC = newViewController; }else{ _currentVC = oldViewController; } }];} //移除所有子視圖控制器- (void)removeAllChildViewControllers{ for (UIViewController *vc in self.childViewControllers) { [vc willMoveToParentViewController:nil]; [vc removeFromParentViewController]; }} /** * 方法說明: * 1、addChildViewController:向父VC中添加子VC,添加之後自動調用willMoveToParentViewController:父VC * 2、removeFromParentViewController:將子VC從父VC中移除,移除之後自動調用 didMoveToParentViewController:nil * 3、willMoveToParentViewController: 當向父VC添加子VC之後,該方法會自動調用。若要從父VC移除子VC,需要在移除之前調用該方法,傳入參數nil。 * 4、didMoveToParentViewController: 當向父VC添加子VC之後,該方法不會被自動調用,需要顯示調用告訴編譯器已經完成添加(事實上不調用該方法也不會有問題,不太明白); 從父VC移除子VC之後,該方法會自動調用,傳入的參數為nil,所以不需要顯示調用。 */ /** * 注意點: 要想切換子視圖控制器a/b,a/b必須均已添加到父視圖控制器中,不然會報錯 */@end
最終效果:(實現了3個視圖之間的切換)
IOS開發—通過ChildViewController實現view的切換