Block作为属性、参数使用

2019-06-14  本文已影响0人  114105lijia

做开发这么久,总结了block常用的3个场景:

1.属性传值;
2.作为参数回调传值;
3.作为参数产值,等待其他操作后在进行回调。(这个用的比较少,但是也有不少场景需要用到。比如请求数据时,必须先等数据请求回来后在进行回调;再比如保存图片到相册,也必须等保存成功后在进行回调告知用户是否保存成功。。)

下面做了一个小demo,直接上代码:
ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

ViewController.m

#import "ViewController.h"
#import "TestViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"first";
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 100, 44);
    [self.view addSubview:btn];
    btn.backgroundColor = [UIColor yellowColor];
    [btn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
}

- (void)next {
    TestViewController *vc = [TestViewController new];
    [self.navigationController pushViewController:vc animated:YES];
    
    vc.propertyBlock = ^(NSInteger index) {
        NSLog(@"property---%ld",index);
    };
    
    [vc setBaseIndex:5 withBackBlock:^(NSInteger index) {
        NSLog(@"directly---%ld",index);
    }];
    
    [vc afterBackblock:^(NSInteger index) {
        NSLog(@"after---%ld",index);
    }];
}

@end

TestViewController.h

#import <UIKit/UIKit.h>

typedef void(^BackBlock)(NSInteger index);
typedef void(^PropertyBlock)(NSInteger index);

NS_ASSUME_NONNULL_BEGIN

@interface TestViewController : UIViewController

@property (nonatomic, copy) BackBlock backBlock;

//属性回调
@property (nonatomic, copy) PropertyBlock propertyBlock;

//作为参数回调
- (void)setBaseIndex:(NSInteger)index withBackBlock:(BackBlock)block;

//等待其他操作,操作结束后在返回block
- (void)afterBackblock:(BackBlock)block;

@end

NS_ASSUME_NONNULL_END

TestViewController.h

#import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"测试";
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 100, 44);
    [self.view addSubview:btn];
    btn.backgroundColor = [UIColor redColor];
    [btn addTarget:self action:@selector(dodo) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(100, 300, 100, 44);
    [self.view addSubview:btn1];
    [btn1 setTitle:@"返回" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn1.backgroundColor = [UIColor yellowColor];
    [btn1 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
}

- (void)back {
    if (self.propertyBlock) {
        self.propertyBlock(1);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dodo {
    if (self.backBlock) {
        self.backBlock(5);
    }
}

- (void)afterBackblock:(BackBlock)block {
    self.backBlock = block;
}

- (void)setBaseIndex:(NSInteger)index withBackBlock:(BackBlock)block {
    NSInteger t = index * index;
    block(t);
}

@end
上一篇 下一篇

猜你喜欢

热点阅读