先不说废话, 上效果图, 代码量也不大, 也不上传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实现订单页的切换的更多相关文章

  1. iOS开发之多表视图滑动切换示例(仿"头条"客户端)---优化篇

    前几天发布了一篇iOS开发之多表视图滑动切换示例(仿"头条"客户端)的博客,之所以写这篇博客,是因为一位iOS初学者提了一个问题,简单的写了个demo做了个示范,让其在基础上做扩展 ...

  2. iOS开发-ViewController的生命周期和切换

    ViewController在App开发中是至关重要的一环,无论是页面的展示和数据之间的交互,ViewController提供了一个框架可以管理和构建App应用.iOS中构建App提供了两种方式一种是 ...

  3. 【转】 iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)

    原文:http://blog.csdn.net/hmt20130412/article/details/34523235 本来只是打算介绍一下addChildViewController这个方法的,正 ...

  4. iOS开发 剖析网易新闻标签栏视图切换(addChildViewController属性介绍)

    本文转载至 http://www.tuicool.com/articles/3ymMzub CSDN博客原文  http://blog.csdn.net/hmt20130412/article/det ...

  5. iOS 开发笔记-控制器翻页

    找了一天,终于找到了两个能用的. 1.https://github.com/wangmchn/WMPageController 2.https://github.com/everettjf/EVTTa ...

  6. 我的iOS开发系列博文

    之前目录性的总结了发表过的关于OC方面的文章,今天在目录性的总结一下有关iOS开发的文章.走过路过不要错过哦,今天的博文也全都是干货.写技术博客与大家交流一下思想也是不错的. 下面是我的技术博客中有关 ...

  7. iOS开发系列--视图切换

    概述 在iOS开发中视图的切换是很频繁的,独立的视图应用在实际开发过程中并不常见,除非你的应用足够简单.在iOS开发中常用的视图切换有三种,今天我们将一一介绍: UITabBarController ...

  8. iOS开发 横向分页样式 可左右滑动或点击头部栏按钮进行页面切换

    iOS开发 横向分页样式 可左右滑动或点击头部栏按钮进行页面切换 不多说直接上效果图和代码 1.设置RootViewController为一个导航试图控制器 //  Copyright © 2016年 ...

  9. iOS开发之——从零开始完成页面切换形变动画

    前言 某天我接到了UI发给我的两张图: 需求图.png 看到图的时候我一脸懵逼,显然我需要做一个页面切换的指示动画.老实说,从大三暑假开始做iOS开发也一年有余了,但是遇到复杂动画总是唯恐避之不及,只 ...

随机推荐

  1. 【趣事】用 JavaScript 对抗 DDOS 攻击 (下)

    上一篇:http://www.cnblogs.com/index-html/p/js-network-firewall.html 对抗 v2 之前的那些奇技淫巧,纯属娱乐而已,并不能撑多久. 但简单. ...

  2. 算法与数据结构(八) AOV网的关键路径

    上篇博客我们介绍了AOV网的拓扑序列,请参考<数据结构(七) AOV网的拓扑排序(Swift面向对象版)>.拓扑序列中包括项目的每个结点,沿着拓扑序列将项目进行下去是肯定可以将项目完成的, ...

  3. 使用cmake自动构建工程

    公司引擎是用cmake根据目标平台来构建工程的,刚接触的时候深深体会到cmake的方便:如果目标平台是windows,它可以帮你自动构建出vs工程:如果是安卓,自动构建出eclipse工程,如果是IO ...

  4. Entity Framework的启动速度优化

    最近开发的服务放到IIS上寄宿之后,遇到一些现象,比如刚部署之后,第一次启动很慢:程序放置一会儿,再次请求也会比较慢.比如第一个问题,可以解释为初次请求某一个服务的时候,需要把程序集加载到内存中可能比 ...

  5. thinkphp数据的查询和截取

    public function NewsList(){ $this->assign('title','news'); $p = I('page',1); $listRows = 6; $News ...

  6. APEX:对object中数据进行简单处理?

    在Salesforce中,常常要对各种数据进行处理,已满足业务逻辑.本篇文章会介绍如何实现从object获取数据,然后将取得的数据进行一系列简单处理. 第一步:SongName__c 是一个新建的ob ...

  7. App 审核由于 IPv6 网络问题被拒

    昨天 提交App Store 的时候被拒了 We discovered one or more bugs in your app when reviewed on iPhone running iOS ...

  8. Android中通过ActionBar为标题栏添加搜索以及分享视窗

    在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果.Action ...

  9. 什么是英特尔® Edison 模块?

    英特尔® Edison 模块 是一种 SD 卡大小的微型计算芯片,专为构建物联网 (IoT) 和可穿戴计算产品而设计. Edison 模块内含一个高速的双核处理单元.集成 Wi-Fi*.蓝牙* 低能耗 ...

  10. ola.hallengren的SQL Server维护脚本

    ola.hallengren的SQL Server维护脚本 下载地址 http://files.cnblogs.com/files/lyhabc/ola.hallengrenMaintenanceSo ...