参考文:
Block形式
返回类型 (^名字) (返回参数列) = ^ (形参列){}
带参数名,无返回,无参数Block
void(^myBlock)(void)=^(void){
NSLog(@"无返回值,无参数");
};
myBlock();
带参数名,无返回,有参数Block
void(^myBlock2)(int,int)=^(int a,int b){
NSLog(@"a+b=%d",a+b);
};
myBlock2(4,6);
有参数名,有返回参数,有参数的的Block
int(^myBlock3)(int,int)=^(int a,int b){
return a+b
};
NSLog(@"%d",myBlock3(4,5));
有参数名,有返回 无参数的Block
int(^my1)(void)=^(void){
return 2;
};
使用typedef定义块类型
复用块类型,使用块类型可以重复定义多个块变量
使用块类型定义函数参数,这样即可定义带块参数的函数
typedef 块返回值类型 (^块类型)(形参类型1,参数类型2)
typedef void(^FKPrintBlock)(NSString*);
FKPrintBlock print =^(NSString *info){
NSLog(@"%@",info);
};
修改block之外的变量
通常不定义成__block ,在block中修改外部变量会出错。
__block int a = 0
void (^foo)(void)=^{
a=1;
};
foo()
使用:Block做属性
A.h
#import <UIKit/UIKit.h>
typedef void(^Height)(CGFloat height);
@interface YKYJLYiView : UIView
@property(nonatomic,copy)Height blockHeight;
@end
-(instancetype)init{
self = [super init];
if (self) {
self.blockHeight(100);
}
return self;
}
B.m
-(instancetype)init{
self = [super init];
if (self) {
A *a = [A alloc]init];
a.blockHeight=^(CGFloat height){
NSLog("%lf",height);
};
}
return self;
}
Block做方法
我常用这东西做网络请求的模块,封装在一个单例中,进行category区分网络请求。
+(void)UserInfoResponse:(void(^)(id success,NSError *error))response;
+(void)UserInfoResponse:(void(^)(id success,NSError *error))response{
NSString *url = [CalendarLocation stringByAppendingPathComponent:@"MySelf/info"];
[[NetWorkTools shardTools]requestMethod:GET URLString:url parameters:nil fileshed:^(id responseObject, NSError *error) {
response(responseObject,error);
}];
}
[YKServiceTools UserInfoResponse:^(id success, NSError *error) {
NSLog(@"%@",success);
}];
Block做方法【网络请求类】
用于管理所有网络请求,也可以创建类别来分别管理请求
h文件 ApplicationService.h
+(void)getJiXiong:(NSDate *)date success:(void(^)(id responseObject))success failure:(void(^)(NSError *error))failure;
m文件 ApplicationService.m
+(void)getJiXiong:(NSDate *)date success:(void(^)(id responseObject))success failure:(void(^)(NSError *error))failure{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd";
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
NSString *dateStr = [dateFormatter stringFromDate:date];
NSMutableDictionary *param = [NSMutableDictionary dictionary];
param[@"rq"] = dateStr;
//这里进行了AFN网络请求,然后进行block传参 这里我自己的请求方式
[[NetWorkTools shardTools]requestMethod:GET URLString:@"我的url" parameters:param fileshed:^(id responseObject, NSError *error) {
success(responseObject);
failure(error);
}];
}
Block做view中button回调至VC
实用于,VC中一个View中某一个按钮点击事件,需要到VC去处理事件(view不能拿到vc的类)使用Block进行传递来实现操作。
typedef void(^ButtonBlock)(CGFloat maxY);
@interface YKExclusiveTodayNotVipView : UIView
@property(copy,nonatomic)ButtonBlock block;
-(void)becomeVipClick:(ButtonBlock)become;
@end
[becomeVipBtn addTarget:self action:@selector(becomeVipClick) forControlEvents:UIControlEventTouchUpInside];
-(void)becomeVipClick{
if (self.block) {
self.block(0);
}
}
-(void)becomeVipClick:(ButtonBlock)become{
self.block = become;
}
[self.todayNotVipView becomeVipClick:^(CGFloat maxY) {
}];