黑魔法:iOS之Block

Block形式

返回类型 (^名字) (返回参数列) = ^ (形参列){}

1. 带参数名,无返回,无参数Block

void(^myBlock)(void)=^(void){
    NSLog(@"无返回值,无参数");
};

myBlock();

2. 带参数名,无返回,有参数Block

void(^myBlock2)(int,int)=^(int a,int b){
     NSLog(@"a+b=%d",a+b);
};

myBlock2(4,6);

3. 有参数名,有返回参数,有参数的的Block

int(^myBlock3)(int,int)=^(int a,int b){
    return a+b
};

NSLog(@"%d",myBlock3(4,5));

4. 有参数名,有返回 无参数的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做方法【网络请求类】

用于管理所有网络请求,也可以创建类别来分别管理请求

ApplicationService.h

+(void)getJiXiong:(NSDate *)date success:(void(^)(id responseObject))success failure:(void(^)(NSError *error))failure;

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) {

    }];

参考文:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值