簡(jiǎn)述:block 與 函數(shù)類似设拟。同時(shí)可以直接用作回調(diào)。
一般當(dāng)做函數(shù)使用
- 實(shí)際使用帘撰,感覺比較少,一般用在回調(diào)核行,代替delegate蹬耘,少些很多代碼,同時(shí)會(huì)帶來一些奇怪的問題综苔。注意循環(huán)引用等一些block
的疑難雜癥。
// block 返回 *(^block 函數(shù)名)(參數(shù)類型) = ^(參數(shù)名){ return }
NSString *(^testBlock)(NSString *) = ^(NSString *name){
return [NSString stringWithFormat:@"我的名字是:%@",name];
};
NSString *testString = testBlock(@"張三");
NSLog(@"%@",testString);
作為回調(diào) 代替delegate
使用 button 點(diǎn)擊時(shí)間回調(diào)舉例
- 1 定義 block 回調(diào)函數(shù)
// 簡(jiǎn)單實(shí)現(xiàn) view 上有2個(gè)button堡牡,點(diǎn)擊時(shí)間回調(diào)晤柄,一個(gè) ok 一個(gè) cancel
typedef NS_ENUM(NSUInteger, selectButton) {
selectButtonOK,
selectButtonCancle
};
// 定義 block
typedef void(^selectButtonBlock)(selectButton buttonIndex);
@interface BlockView : UIView
// 回調(diào)方法
-(void)setSelectBolck:(selectButtonBlock )block;
// ******** eg ******** //
// 為了 更簡(jiǎn)化上面的 代碼,可以更直接可免,直接使用copy 的block 做粤,供外部調(diào)用,type 類型直接用path傳怕品,注意不是很安全。這個(gè)不詳細(xì)介紹闯估。
@property (nonatomic, copy) void(^myBlock)(NSInteger path);
- 2 創(chuàng)建 2個(gè)button 連接點(diǎn)擊事件
UIButton *okButton = [UIButton buttonWithType:UIButtonTypeSystem];
okButton.frame = CGRectMake(0, 0, 100, 100);
[okButton addTarget:self action:@selector(okButtonAction) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:okButton];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
cancelButton.frame = CGRectMake(0, 100, 100, 100);
[cancelButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:cancelButton];
- 3 使用block 鏈接button 點(diǎn)擊事件
// 需要一個(gè) 全局 block吼和,將外部穿進(jìn)來的 block賦值
static selectButtonBlock TempBlock;
-(void)setSelectBolck:(selectButtonBlock)block{
if (block) {
TempBlock = block;
}
}
-(void)okButtonAction{
if (TempBlock) {
TempBlock(selectButtonOK);
}
}
-(void)cancelButtonAction{
if (TempBlock) {
TempBlock(selectButtonCancle);
}
}
- 4 外部使用 舉例
BlockView *testView = [[BlockView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
// block button 點(diǎn)擊事件 回調(diào)
[testView setSelectBolck:^(selectButton buttonIndex) {
if (buttonIndex == selectButtonOK) {
NSLog(@"ok");// 對(duì)應(yīng) 點(diǎn)擊 ok button
} else if (buttonIndex == selectButtonCancel){
NSLog(@"cancel");//對(duì)應(yīng) 點(diǎn)擊 cancel button
} else {
NSLog(@"其他 error");
}
}];
類似AFN 判斷 回調(diào)炫乓,或者 錯(cuò)誤回調(diào)
- 1 簡(jiǎn)單 舉例 2 個(gè)數(shù)相加 是否大于100,大于100返回較大的值末捣,不大于100返回較小的值。
+(void)addBackInt1:(NSInteger)int1 int2:(NSInteger)int2 success:(void (^)(NSInteger max))success failure:(void (^)(NSInteger min))failure;
+(void)addBackInt1:(NSInteger)int1 int2:(NSInteger)int2 success:(void (^)(NSInteger))success failure:(void (^)(NSInteger))failure{
if (int1 + int2 > 100) {
if (success) {
success(int1 > int2 ?int1:int2);
}
}else{
if (failure) {
failure(int1 > int2 ?int2:int1);
}
}
}
- 2 外部使用 判斷回調(diào)
[blockView addBackInt1:14 int2:80 success:^(NSInteger max) {
NSLog(@"%zi",max);
} failure:^(NSInteger min) {
NSLog(@"%zi",min);
}];
關(guān)于 block 使用需要注意的 一些問題
- 循環(huán)引用
- block 對(duì)局部變量只讀
其他
待補(bǔ)充