iOS开发--ChildViewController实现订单页的切换
先不说废话, 上效果图, 代码量也不大, 也不上传github骗星星了, 你们复制粘贴下代码, 就可以轻而易举的弄出一个小demo.

这个代码的实现并不复杂, 甚至于说非常简单, 就是逻辑有点小绕, 下面将用代码详细讲解下.
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 = ; i < ; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake( / * i, , / , self.H);
[button setTitle:nameArray[i] forState:UIControlStateNormal];
button.tag = + i;
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor grayColor];
button.titleLabel.textColor = [UIColor redColor];
button.titleLabel.font = [UIFont systemFontOfSize:];
[self addSubview:button];
}
} - (void)buttonAction:(UIButton *)button{
NSLog(@"第%ld个按钮被点击了", button.tag -);
_block(button.tag - );
}
@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(, , , )];
indentLabel.text = @"订单";
indentLabel.font = [UIFont systemFontOfSize:];
[self.contentView addSubview:indentLabel]; //查看全部订单
RootClassLabel *checkIndentLabel = [[RootClassLabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - ( + + + ) / , / , / , / )];
checkIndentLabel.text = @"查看全部订单";
checkIndentLabel.font = [UIFont systemFontOfSize:];
[self.contentView addSubview:checkIndentLabel]; RootClassImageView *imageView = [[RootClassImageView alloc]initWithFrame:CGRectMake(checkIndentLabel.X + checkIndentLabel.W + , checkIndentLabel.Y, , )];
[self.contentView addSubview:imageView]; /**循环实例化imageView对象
*
*待付款
*
*待收货
*
*待评价
*
*退换货
*
*/ for (NSInteger i = ; i < ; i++) {
RootClassImageView *imageView = [[RootClassImageView alloc]initWithFrame:CGRectMake( + / * i, / , / , / )];
//imageView增加tag值
imageView.tag = + 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 - );
} - (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 = ;
}
cell.block = ^(NSInteger index){
//index就是通过block传递过来imageView的tag值.
MyIndentViewController *myIndentViewController = [MyIndentViewController new];
//当点击imageView后, 将图片的tag值传递给vc, vc通过这个值来布局, 以实现点击不同的imageView, 使页面中呈现不同的子视图.
myIndentViewController.index = index + ;
[self.navigationController pushViewController: myIndentViewController animated:];
};
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(, , SCREEN_WIDTH, )];
[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 == ) ||
(_currentViewController == _obligationViewController && sign == ) ||
(_currentViewController == _waitingReceiveViewController && sign == ) ||
(_currentViewController == _waitingEvaluateViewController && sign == ) ||
(_currentViewController == _exchangeViewController && sign == )
) {
return;
}
else{
[self replaceOldViewCroller:_currentViewController newViewController:_viewControllerArray[sign]];
}
} - (void)addViewController {
//视图控制器数组
_viewControllerArray = [NSMutableArray array]; //设置子视图的尺寸
CGRect rect = CGRectMake(, , SCREEN_HEIGHT, SCREEN_HEIGHT - ); //实例化子视图的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 = ; i < ; 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:. 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开发--ChildViewController实现订单页的切换的更多相关文章
- iOS开发之多表视图滑动切换示例(仿"头条"客户端)---优化篇
前几天发布了一篇iOS开发之多表视图滑动切换示例(仿"头条"客户端)的博客,之所以写这篇博客,是因为一位iOS初学者提了一个问题,简单的写了个demo做了个示范,让其在基础上做扩展 ...
- iOS开发-ViewController的生命周期和切换
ViewController在App开发中是至关重要的一环,无论是页面的展示和数据之间的交互,ViewController提供了一个框架可以管理和构建App应用.iOS中构建App提供了两种方式一种是 ...
- 【转】 iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
原文:http://blog.csdn.net/hmt20130412/article/details/34523235 本来只是打算介绍一下addChildViewController这个方法的,正 ...
- iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)
本文转载至 http://www.tuicool.com/articles/3ymMzub CSDN博客原文 http://blog.csdn.net/hmt20130412/article/det ...
- iOS 开发笔记-控制器翻页
找了一天,终于找到了两个能用的. 1.https://github.com/wangmchn/WMPageController 2.https://github.com/everettjf/EVTTa ...
- 我的iOS开发系列博文
之前目录性的总结了发表过的关于OC方面的文章,今天在目录性的总结一下有关iOS开发的文章.走过路过不要错过哦,今天的博文也全都是干货.写技术博客与大家交流一下思想也是不错的. 下面是我的技术博客中有关 ...
- iOS开发系列--视图切换
概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController ...
- iOS开发 横向分页样式 可左右滑动或点击头部栏按钮进行页面切换
iOS开发 横向分页样式 可左右滑动或点击头部栏按钮进行页面切换 不多说直接上效果图和代码 1.设置RootViewController为一个导航试图控制器 // Copyright © 2016年 ...
- iOS开发之——从零开始完成页面切换形变动画
前言 某天我接到了UI发给我的两张图: 需求图.png 看到图的时候我一脸懵逼,显然我需要做一个页面切换的指示动画.老实说,从大三暑假开始做iOS开发也一年有余了,但是遇到复杂动画总是唯恐避之不及,只 ...
随机推荐
- ImageView缩放选项
ImageView.ScaleType 将图片边界缩放到所在view边界时的缩放选项. Options for scaling the bounds of an image to the bounds ...
- SQL Server 数据加密功能解析
SQL Server 数据加密功能解析 转载自: 腾云阁 https://www.qcloud.com/community/article/194 数据加密是数据库被破解.物理介质被盗.备份被窃取的最 ...
- 06.SQLServer性能优化之---数据库级日记监控
汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 之前说了一下数据库怎么发邮件:http://www.cnblogs.com/duniti ...
- 使用Java原生代理实现AOP
### 本文由博主柒.原创,转载请注明出处 ### 完整源码下载地址 [https://github.com/MatrixSeven/JavaAOP](https://github.com/Matri ...
- nodejs利用http模块实现银行卡所属银行查询和骚扰电话验证
http模块内部封装了http服务器和客户端,因此Node.js不需要借助Apache.IIS.Nginx.Tomcat等传统HTTP服务器,就可以构建http服务器,亦可以用来做一些爬虫.下面简单介 ...
- 卸载oracle之后,如何清除注册表
之前卸载了oracle,今天偶然间发现,在服务和应用程序里面,还残存着之前的oracle服务.原来,还需要去清理下注册表. 在开始菜单的这个框里面 输入regedit,进入注册表.找到这个目录 HKE ...
- Android之网络数据存储
一.网络保存数据介绍 可以使用网络来保存数据,在需要的时候从网络上获取数据,进而显示在App中. 用网络保存数据的方法有很多种,对于不同的网络数据采用不同的上传与获取方法. 本文利用LeanCloud ...
- [Hadoop in Action] 第7章 细则手册
向任务传递定制参数 获取任务待定的信息 生成多个输出 与关系数据库交互 让输出做全局排序 1.向任务传递作业定制的参数 在编写Mapper和Reducer时,通常会想让一些地方可以配 ...
- 烂泥:redis3.2.3安装与配置
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 前一段时间写过一篇codis集群的文章,写那篇文章主要是因为当时的项目不支持redis自 ...
- 1 selenium3.0.1无法打开火狐浏览器
[问题描述] 1.配置selenium3.0和java后,尝试打开火狐浏览器,提示缺少geckodriver驱动. [解决方案] 1.在http://www.seleniumhq.org/downlo ...