iOS开发--ChildViewController实现订单页的切换

时间:2021-12-04 19:53:01

  先不说废话, 上效果图, 代码量也不大, 也不上传github骗星星了, 你们复制粘贴下代码, 就可以轻而易举的弄出一个小demo.

  iOS开发--ChildViewController实现订单页的切换

  这个代码的实现并不复杂, 甚至于说非常简单, 就是逻辑有点小绕, 下面将用代码详细讲解下.

  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

  通过这么简单的几步, 就实现了如上界面.