參考文:
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) {
}];