IOS开发—通过ChildViewController实现view的切换
来源:互联网 发布:彩票app源码 编辑:程序博客网 时间:2024/06/05 11:01
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个视图之间的切换)
0 0
- IOS开发—通过ChildViewController实现view的切换
- IOS开发—通过ChildViewController实现view的切换
- iOS 在同一个页面中,通过childViewController切换界面
- iOS开发 UIViewController的childViewController相关
- iOS -- childViewController 的问题
- iOS: 通过addChildViewController实现 Segment 下的 View 的切换
- iOS: 通过addChildViewController实现 Segment 下的 View 的切换
- iOS: 通过addChildViewController实现 Segment 下的 View 的切换
- IOS实现多个View的切换
- ios开发(十四)view之间的切换
- iOS ChildViewController详解
- [iOS]通过UIScrollView和UIPageControl实现滑动切换的效果
- 如何通过代码添加ChildViewController
- android 通过setContentView切换Activity的View
- ChildViewController的应用
- ChildViewController
- ChildViewController
- 【iOS开发-24】导航控制器下不同视图控制器之间切换:利用CATrasition和view的layer层来实现自定义的动画效果
- 云时代的编程模式
- hive索引
- lintcode ----比较字符串
- spring MVC +Spring + Hibernate + PostgreSQL框架的集成和多租户( 一)
- linux 下 iozone随机读写测试报错: error reading block
- IOS开发—通过ChildViewController实现view的切换
- sort b
- UVA 11983-Weird Advertisement-k次以上覆盖矩形面积(线段树+扫描线)
- Autolayout之 关于+requiresConstraintBasedLayout方法作用
- 【leetcode】172. Factorial Trailing Zeroes
- LeetCode 40 Combination Sum II
- Robot Framework自动化测试框架(三):Android自动化
- Recording HTTP traffic on iOS devices with HP LoadRunner
- 如何将pdf转换成word格式可编辑的格式