參考文檔:Block實(shí)現(xiàn)iOS回調(diào)
一、Block的兩種申明方法:
- 方法1
#import <UIKit/UIKit.h>
typedef void(^YDBlock)(void);
@interface ViewController : UIViewController
@property(nonatomic,copy)YDBlock blockOne;
@end
- 方法2
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(nonatomic,copy)void(^ydBlock)(void);
@end
二、Block的常見用途
a、Block傳遞cell上的按鈕點(diǎn)擊事件
1)、在cell的.h文件申明block
//聲明一個(gè)名為 AddToCartsBlock 無返回值,參數(shù)為XSMyFavoriteTableViewCell 類型的block
@class LSXCommunityCell;
typedef void (^ReportBlock) (LSXCommunityCell *);
@interface LSXCommunityCell : UITableViewCell
//“舉報(bào)”按鈕點(diǎn)擊事件的block
@property(nonatomic, copy) ReportBlock ReportBlock;
2)、在cell.m文件里面的按鈕的點(diǎn)擊事件里面調(diào)用或賦值
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if([super initWithStyle:style reuseIdentifier:reuseIdentifier]){
listBtn=[[UIButton alloc]init];
[listBtn addTarget:self action:@selector(listBtnClick) forControlEvents:UIControlEventTouchUpInside];
NSArray *views = @[listBtn];
[self.contentView sd_addSubviews:views]; listBtn.sd_layout.topEqualToView(iconView).widthIs(15).heightIs(15).rightSpaceToView(timeLa, 0);
}
return self;
}
#pagrama mark-按鈕的點(diǎn)擊事件
-(void)listBtnClick{
if (self.ReportBlock) {
self.ReportBlock(self);
}
}
3)、在視圖控制器中的創(chuàng)建cell的代理方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中給Block賦值調(diào)用它的setter方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 通過indexPath創(chuàng)建cell實(shí)例 每一個(gè)cell都是單獨(dú)的
// LSXCommunityCell *cell = [tableView cellForRowAtIndexPath:indexPath];
LSXCommunityCell *cell=[tableView dequeueReusableCellWithIdentifier:_Identifier];
[[cell viewWithTag:100] removeFromSuperview];
if (cell == nil) {
//重構(gòu)Cell的時(shí)候,通過 _Identifier判斷是否創(chuàng)建打電話按鈕
cell = [[LSXCommunityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:_Identifier];
}
[[cell viewWithTag:100] removeFromSuperview];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.delegate=self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
if(self.dataArray.count>0){
_model =self.dataArray[indexPath.row];
cell.Model=_model;
__weak typeof(self) weakSelf = self;
cell.ReportBlock = ^(LSXCommunityCell *cell) {
[weakSelf Report:cell];
};
cell.CommunityIdBlock = ^(NSString *str) {
_idStr =str;
};
cell.bjbrPhoneBlock = ^(NSString *str) {
_bjbrPhoneStr=str;
};
}
return cell;
}
b、兩個(gè)頁面之間傳值
1)、在第二個(gè)頁面的.h文件申明block
typedef void (^DeleteButtonBlock) (NSString*);
@property (nonatomic,copy) DeleteButtonBlock deleteButtonBlock;
2)、在返回頁面跳轉(zhuǎn)的時(shí)候傳值,所以在cell.m文件里面的按鈕的點(diǎn)擊事件里面調(diào)用或賦值
-(void)Click{
if (_deleteButtonBlock) {
_deleteButtonBlock(self.name);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
3)、在第一個(gè)視圖控制器中的頁面跳轉(zhuǎn)中給Block賦值調(diào)用它的setter方法
-(void)Click{
ViewController2 *vc=[[ViewController2 alloc]init];
//用到self的地方,強(qiáng)引用,防止內(nèi)存泄漏
__block ViewController* bself = self;
vc.deleteButtonBlock = ^(NSString *name) {
bself.lable.text=name;
};
[self presentViewController:vc animated:YES completion:nil];
}
三、心得:
- 賦值最好使用
if (_kanBlock) {
self.kanBlock();
}