先不说废话, 上效果图, 代码量也不大, 也不上传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之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  2. tLinux 2.2下安装Mono 4.8

    Tlinux2.2发行版基于CentOS 7.2.1511研发而成,内核版本与Tlinux2.0发行版保持完全一致,更加稳定,并保持对Tlinux2.0的完全兼容.Mono 4版本要求CentOS 7 ...

  3. MVVM模式和在WPF中的实现(二)数据绑定

    MVVM模式解析和在WPF中的实现(二) 数据绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  4. Linux碎碎念

    在学习Linux过程中,有许多有用的小技巧.如果放在纸质的笔记本上,平时查阅会相当不方便.现在以一种“碎碎念”的方式,汇集整理在此,目前还不是很多,但随着学习.工作的深入,后续会陆陆续续添加更多的小技 ...

  5. ZKWeb网页框架1.4正式发布

    本次更新的内容有 添加更快的批量操作函数 添加IDatabaseContext.FastBatchSave 添加IDatabaseContext.FastBatchDelete 注意这些函数不会触发注 ...

  6. PHP中PDO事务的使用方法

    事务 (Transaction) 是操作数据库中很重要的一个功能, 它可以让你预定一条, 或者一系列 SQL 语句, 然后一起执行. 在执行的过程中, 如果其中的某条执行失败, 可以回滚所有已更改的操 ...

  7. [原]Paste.deploy 与 WSGI, keystone 小记

    Paste.deploy 与 WSGI, keystone 小记 名词解释: Paste.deploy 是一个WSGI工具包,用于更方便的管理WSGI应用, 可以通过配置文件,将WSGI应用加载起来. ...

  8. 火星坐标、百度坐标、WGS-84坐标相互转换及墨卡托投影坐标转经纬度JavaScript版

    火星坐标 火星坐标是国家测绘局为了国家安全在原始坐标的基础上进行偏移得到的坐标,基本国内的电子地图.导航设备都是采用的这一坐标系或在这一坐标的基础上进行二次加密得到的.火星坐标的真实名称应该是GCJ- ...

  9. C# 序列化与反序列化几种格式的转换

    这里介绍了几种方式之间的序列化与反序列化之间的转换 首先介绍的如何序列化,将object对象序列化常见的两种方式即string和xml对象; 第一种将object转换为string对象,这种比较简单没 ...

  10. BPM配置故事之案例1-配置简单流程

    某天,Boss找到了信息部工程师小明. Boss:咱们新上了H3 BPM,你研究研究把现在的采购申请流程加上去吧,这是采购申请单. 小明:好嘞 采购申请单 小明回去后拿着表单想了想,开始着手配置. 他 ...