block常见写法总结

2017年6月22日
一.常用写法(子页面回调给父页面)
1.子类cell视图

typedef void(^blk_orderListModel)(HuOrderListModel *);
@property (nonatomic, copy) blk_orderListModel payClick;
self.payClick(_dataModel);

2.父类试图

            cell.payClick = ^(HuOrderListModel *orderListModel) {
//                [weakSelf enterPayFailurePage:orderListModel];
            };

2017年6月6日
一.ARC模式下 属性定义类型
1.block 一般定义成copy

用法1. 当前页面A 给其他页面 回调
A页面代码

typedef void(^blk_t)(void);
@property (nonatomic, copy) blk_t changeColorTimeOut;
- (void)setChangeColorTimeOut:(blk_t)blk
{
    if (_changeColorTimeOut == nil) {
        _changeColorTimeOut = blk;
        _needChangeColor = YES;
    }
}
- (void)createCountTime
{
//......
    if (_changeColorTimeOut) {
       self.changeColorTimeOut();//修改颜色
       _needChangeColor = NO;
    }
}

B页面代码

_coutTimeV.changeColorTimeOut = ^(){
     weakSelf.coutTimeV.timeL.textColor = [HuConfigration uiColorFromString:@"#f66767"];
 };

用法2. 其他页面给A页面数据 【其实也可以直接给,不需要用block,不过这样写就可以少写赋值更新函数,而且可以在inti的时候就回调】
A页面代码

typedef void(^TrainCourseIntroduce) (TrainingModel *model);
@property (nonatomic, copy) TrainCourseIntroduce trainCourseIntroduce;
- (void)viewDidLoad {
    [super viewDidLoad];
    WS(weakSelf);
    self.trainCourseIntroduce = ^(TrainingModel *model) {
        weakSelf.trainModel = model;
        [weakSelf.tableView reloadData];
    };
}

B页面代码

weakSelf.introduceCtrl.trainCourseIntroduce(model);

2017年5月27日
一.两个相关页面都有交卷功能,如何用一个接口实现


block常见写法总结_第1张图片
image.png

1.原理:通过block,将答题卡的交卷动作返回给考试界面实现
2.实现:

//  HuExerciseCardViewController.h
@interface HuExerciseCardViewController : HuViewController
@property (nonatomic, strong) void (^upload)(void);
@end

@implementation HuExerciseCardViewController
- (void)uploadPaper
{
    self.upload();
}

@end

#import "HuTestPracticeViewController.h"
@implementation HuTestPracticeViewController
- (void)enterExerciseCardPage
{
    HuExerciseCardViewController *vc = [[HuExerciseCardViewController alloc] initWithExeriseId:_curId];
   
    WS(weakSelf);
    vc.upload = ^(){
        [weakSelf uploadPaper:NO];
    };
}

- (void)uploadPaper:(BOOL)isTimeOut
{
}
@end

如果您发现本文对你有所帮助,如果您认为其他人也可能受益,请把它分享出去。

你可能感兴趣的:(block常见写法总结)