iOS使用block传值的使用方法

常见的使用场景
1.A控制器push到B控制器, Bpop时需要通知或者传值传值给A控制器
2.A控制器使用B,C等自定义视图,B/C中的交互事件需要通知或者传值给A控制器
3....


使用方法
Test.h文件
@property (nonatomic, copy) void(^test1)(void);  //不传参数
@property (nonatomic, copy) void(^test2)(NSString *);  //传递1个参数
@property (nonatomic, copy) void(^test3)(NSString *, NSString *);  //传递2个参数
@property (nonatomic, copy) NSString *(^test4)(NSString *);  //需要返回值,并且传递1个参数

Test.m文件
//string为传给A的数据
self.test1();
self.test2(tring);
self.test3(string, string);
NSString *string = self.test4(string);
NSLog(@"返回值为:%@", string);

//A控制器
Test *test = [[Test alloc] init];

//self 用weak修饰
__weak typeof (self)weakSelf = self;

test.test1 =  ^() {
};

test.test2 =  ^(NSString *string) {
};

test.test3 =  ^(NSString *string1, NSString *string2) {
};

test.test4 = ^NSString *(NSString *string) {
    NSLog(@"接到传过来的%@", string);
    return @"传给Test";
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值