iOS之Block

參考文:


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

        

    }];
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Block基礎回顧 1.什么是Block? 帶有局部變量的匿名函數(名字不重要,知道怎么用就行),差不多就與C語言...
    Bugfix閱讀 6,793評論 5 61
  • 在iOS 4.0之后,block橫空出世,它本身封裝了一段代碼并將這段代碼當做變量,通過block()的方式進行回...
    Clark_new閱讀 498評論 0 4
  • 前言 ios4.0系統已開始支持block,在編程過程中,block被Obj-C看成是對象,它封裝了一段代碼,這段...
    GitHubPorter閱讀 814評論 2 4
  • 在了解Block之前,我們有必要先了解一下一些基礎知識。我們都知道,Objective-C是由C語言擴展而來的。在...
    SmithJackyson閱讀 720評論 0 8
  • Block是蘋果在iOS4開始引入的對C語言的擴展,用來實現匿名函數的特性,Block是一種特殊的數據類型,其可...
    郭豪豪閱讀 230評論 0 0