iOS开发--ChildViewController实现订单页的切换
2016-03-22 11:59
633 查看
先不说废话, 上效果图, 代码量也不大, 也不上传github骗星星了, 你们复制粘贴下代码, 就可以轻而易举的弄出一个小demo.
这个代码的实现并不复杂, 甚至于说非常简单, 就是逻辑有点小绕, 下面将用代码详细讲解下.
childViewController方便在一个vc上面呈现出这种多种vc的效果, 相信好处百度上面说的多了去了, 这里只说实现.
首先, 需要新建一个父vc和五个子vc, 这里就不多说了, 先给大家看看进入父vc后的上面的btn控件封装, 以及方法回调.
IndentButtonView.h中代码
这里声明了一个具有整形参数的block闭包属性, 用于捕获button的tag值来作为参数回调.
IndentButtonView.m中代码, 有我自己定义的宏和基类, 你们按照你们自己的习惯来写就好.
当点击button的时候, 触发闭包传值回调. 通过button的tag值判断, 在闭包实现的地方也会有不同的结果. 这样一个button按钮条的封装就完成了.
然后是外部的一个自定义cell, 我选择了用四个imageView作为订单四个状态的按钮. 这个自定义cell同样声明了一个闭包属性, 作为以后的回调, 传递的也是imageView的tag值作为参数.
.h
.m中代码
自定义cell在重用池协议中代码, 对闭包进行了实现, 同时传递值给要跳转的vc.
可能上面传tag值让大家很迷茫, 现在把最重要的实现部分的VC代码拿出来, 大家就好懂了.
.h
.m
通过这么简单的几步, 就实现了如上界面.
这个代码的实现并不复杂, 甚至于说非常简单, 就是逻辑有点小绕, 下面将用代码详细讲解下.
childViewController方便在一个vc上面呈现出这种多种vc的效果, 相信好处百度上面说的多了去了, 这里只说实现.
首先, 需要新建一个父vc和五个子vc, 这里就不多说了, 先给大家看看进入父vc后的上面的btn控件封装, 以及方法回调.
IndentButtonView.h中代码
#import "RootClassView.h" typedef void(^buttonBlock)(NSInteger); @interface IndentButtonView : RootClassView @property(nonatomic, copy)buttonBlock block; @end
这里声明了一个具有整形参数的block闭包属性, 用于捕获button的tag值来作为参数回调.
IndentButtonView.m中代码, 有我自己定义的宏和基类, 你们按照你们自己的习惯来写就好.
#import "IndentButtonView.h" #import "ALLHeaderFile.pch" @implementation IndentButtonView - (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { [self createButton]; } return self; } - (void)createButton{ NSArray *nameArray = @[@"全部", @"待付款", @"待收货", @"待评价", @"退换货"]; for (NSInteger i = 0; i < 5; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(150 / 2 * i, 0, 150 / 2, self.H); [button setTitle:nameArray[i] forState:UIControlStateNormal]; button.tag = 2006 + i; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; button.backgroundColor = [UIColor grayColor]; button.titleLabel.textColor = [UIColor redColor]; button.titleLabel.font = [UIFont systemFontOfSize:15]; [self addSubview:button]; } } - (void)buttonAction:(UIButton *)button{ NSLog(@"第%ld个按钮被点击了", button.tag -2006); _block(button.tag - 2006); } @end
当点击button的时候, 触发闭包传值回调. 通过button的tag值判断, 在闭包实现的地方也会有不同的结果. 这样一个button按钮条的封装就完成了.
然后是外部的一个自定义cell, 我选择了用四个imageView作为订单四个状态的按钮. 这个自定义cell同样声明了一个闭包属性, 作为以后的回调, 传递的也是imageView的tag值作为参数.
.h
#import "RootClassTableViewCell.h" #import "RootClassImageView.h" typedef void(^indentBlock)(NSInteger); @interface IndentTableViewCell : RootClassTableViewCell @property(nonatomic, copy)indentBlock block; @end
.m中代码
#import "IndentTableViewCell.h" #import "XCLHeader.h" #import "RootClassLabel.h" @implementation IndentTableViewCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self createCell]; } return self; } - (void)createCell { //订单Label RootClassLabel *indentLabel = [[RootClassLabel alloc]initWithFrame:CGRectMake(48, 18, 30, 15)]; indentLabel.text = @"订单"; indentLabel.font = [UIFont systemFontOfSize:15]; [self.contentView addSubview:indentLabel]; //查看全部订单 RootClassLabel *checkIndentLabel = [[RootClassLabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - (168 + 10 + 14 + 40) / 2, 38 / 2, 168 / 2, 28 / 2)]; checkIndentLabel.text = @"查看全部订单"; checkIndentLabel.font = [UIFont systemFontOfSize:14]; [self.contentView addSubview:checkIndentLabel]; RootClassImageView *imageView = [[RootClassImageView alloc]initWithFrame:CGRectMake(checkIndentLabel.X + checkIndentLabel.W + 5, checkIndentLabel.Y, 7, 14)]; [self.contentView addSubview:imageView]; /**循环实例化imageView对象 * *待付款 * *待收货 * *待评价 * *退换货 * */ for (NSInteger i = 0; i < 4; i++) { RootClassImageView *imageView = [[RootClassImageView alloc]initWithFrame:CGRectMake(41 + 167 / 2 * i, 117 / 2, 85 / 2, 85 / 2)]; //imageView增加tag值 imageView.tag = 2002 + i; //打开imageView交互 imageView.userInteractionEnabled = YES; //为imageView添加轻拍手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]; [imageView addGestureRecognizer:tap]; imageView.t; [self.contentView addSubview:imageView]; } } - (void)tapAction:(UITapGestureRecognizer *)tap { _block(tap.view.tag - 2002); } - (void)layoutSubviews { [super layoutSubviews]; }
自定义cell在重用池协议中代码, 对闭包进行了实现, 同时传递值给要跳转的vc.
//订单 static NSString *indentifier = @"indent"; IndentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier]; if (!cell) { cell = [[IndentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier]; cell.selectionStyle = 0; } cell.block = ^(NSInteger index){ //index就是通过block传递过来imageView的tag值. MyIndentViewController *myIndentViewController = [MyIndentViewController new]; //当点击imageView后, 将图片的tag值传递给vc, vc通过这个值来布局, 以实现点击不同的imageView, 使页面中呈现不同的子视图. myIndentViewController.index = index + 1; [self.navigationController pushViewController: myIndentViewController animated:1]; }; return cell;
可能上面传tag值让大家很迷茫, 现在把最重要的实现部分的VC代码拿出来, 大家就好懂了.
.h
#import "ViewController.h" @interface MyIndentViewController : ViewController @property(nonatomic, assign)NSInteger index; @end
.m
//我的订单页 #import "MyIndentViewController.h" #import "ALLHeaderFile.pch" @interface MyIndentViewController () @property(nonatomic, strong)IndentButtonView *buttonView; //子视图 @property(nonatomic, strong)AllIndentViewController *allIndentViewController; @property(nonatomic, strong)ObligationViewController *obligationViewController; @property(nonatomic, strong)WaitingReceiveViewController *waitingReceiveViewController; @property(nonatomic, strong)WaitingEvaluateViewController *waitingEvaluateViewController; @property(nonatomic, strong)ExchangeViewController *exchangeViewController; //当前视图 @property(nonatomic, strong)RootClassViewController *currentViewController; //视图控制器数组 @property(nonatomic, strong)NSMutableArray *viewControllerArray; @end @implementation MyIndentViewController - (void)loadView { [super loadView]; //实例化buttonView _buttonView = [[IndentButtonView alloc]initWithFrame:CGRectMake(0, 69, SCREEN_WIDTH, 30)]; [self.view addSubview:_buttonView]; //添加视图方法 [self addViewController]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //实现buttonView的block __weak typeof (self) WeakSelf = self; _buttonView.block = ^(NSInteger sign){ [WeakSelf changeChildViewController:sign]; }; } - (void)changeChildViewController:(NSInteger)sign { //通过block传递过来的tag值判断切换视图 //如果点击的button在当前页, 则废弃点击操作 if ((_currentViewController == _allIndentViewController && sign == 0) || (_currentViewController == _obligationViewController && sign == 1) || (_currentViewController == _waitingReceiveViewController && sign == 2) || (_currentViewController == _waitingEvaluateViewController && sign == 3) || (_currentViewController == _exchangeViewController && sign == 4) ) { return; } else{ [self replaceOldViewCroller:_currentViewController newViewController:_viewControllerArray[sign]]; } } - (void)addViewController { //视图控制器数组 _viewControllerArray = [NSMutableArray array]; //设置子视图的尺寸 CGRect rect = CGRectMake(0, 109, SCREEN_HEIGHT, SCREEN_HEIGHT - 109); //实例化子视图的vc _allIndentViewController = [AllIndentViewController new]; _obligationViewController = [ObligationViewController new]; _waitingReceiveViewController = [WaitingReceiveViewController new]; _waitingEvaluateViewController = [WaitingEvaluateViewController new]; _exchangeViewController = [ExchangeViewController new]; //将子视图的vc添加到一个可变数组中, 方便处理 [_viewControllerArray addObject:_allIndentViewController]; [_viewControllerArray addObject:_obligationViewController]; [_viewControllerArray addObject:_waitingReceiveViewController]; [_viewControllerArray addObject:_waitingEvaluateViewController]; [_viewControllerArray addObject:_exchangeViewController]; //偷懒 for (NSInteger i = 0; i < 5; i++) { [_viewControllerArray[i] view].frame = rect; } //这块是实现能够在外面点击不同的imageView进入不同页面的关键, 通过属性传值确定视图的内部布局 [self.view addSubview:[_viewControllerArray[_index] view]]; //将当前子视图设置为传值确定的子视图 _currentViewController = _viewControllerArray[_index]; //将子视图添加到父视图上 [self addChildViewController:_viewControllerArray[_index]]; } #pragma mark 切换子视图方法 - (void)replaceOldViewCroller:(RootClassViewController *)oldViewController newViewController:(RootClassViewController *)newViewController{ //将新的子视图先添加到父视图上 [self addChildViewController:newViewController]; //这个方法是负责对子视图进行切换的, 有几个参数, 前两个参数是切换前子视图和切换后子视图, 这个方法有个条件, 就是一定要两个视图都是当前父视图的子视图才可以切换, 所以在上面才会先添加子视图, 后面的参数都应该很熟悉了, duration延时, options选项, 可以将动画的枚举类型给他, animations更不用说了, 动画效果, 闭包的bool参数finish代表的是切换是否成功 [self transitionFromViewController:oldViewController toViewController:newViewController duration:.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) { if (finished) { //切换后将老视图移除, 新的视图设置为当前视图 [oldViewController removeFromParentViewController]; _currentViewController = newViewController; }else{ _currentViewController = oldViewController; } }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
通过这么简单的几步, 就实现了如上界面.
相关文章推荐
- iOS实现文字转化成彩色文字图片
- iOS隐藏某一界面自定义的返回按钮
- 需求 - 20 - 提示框
- iOS 上线加急审核流程
- iOS 点空白处回收键盘
- iOS自定义控件-动态标签展示
- iOS-self.layer.shouldRasterize属性
- iOS代码拾遗录--调试几个小技巧
- 获取iOS设备型号的方法总结
- iOS实现双向滑动条效果
- iOS如何使用自己添加的字体库
- 解决ios模拟器不能弹出键盘问题的方法
- ios 中tableview和scrollView的区别
- 汇总ios开发逆向传值的方法
- 一步一步教你做IOS推送
- iOS自定义button抖动效果并实现右上角删除按钮
- 仿陌陌的ios客户端+服务端源码项目
- 使用Xcode和Instruments调试解决iOS内存泄露
- 基于Udp的socket 实现
- iOS开发中文件的上传和下载功能的基本实现-备用