参考文档:Block实现iOS回调
一、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上的按钮点击事件
1)、在cell的.h文件申明block
//声明一个名为 AddToCartsBlock 无返回值,参数为XSMyFavoriteTableViewCell 类型的block
@class LSXCommunityCell;
typedef void (^ReportBlock) (LSXCommunityCell *);
@interface LSXCommunityCell : UITableViewCell
//“举报”按钮点击事件的block
@property(nonatomic, copy) ReportBlock ReportBlock;
2)、在cell.m文件里面的按钮的点击事件里面调用或赋值
-(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-按钮的点击事件
-(void)listBtnClick{
if (self.ReportBlock) {
self.ReportBlock(self);
}
}
3)、在视图控制器中的创建cell的代理方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中给Block赋值调用它的setter方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 通过indexPath创建cell实例 每一个cell都是单独的
// LSXCommunityCell *cell = [tableView cellForRowAtIndexPath:indexPath];
LSXCommunityCell *cell=[tableView dequeueReusableCellWithIdentifier:_Identifier];
[[cell viewWithTag:100] removeFromSuperview];
if (cell == nil) {
//重构Cell的时候,通过 _Identifier判断是否创建打电话按钮
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、两个页面之间传值
1)、在第二个页面的.h文件申明block
typedef void (^DeleteButtonBlock) (NSString*);
@property (nonatomic,copy) DeleteButtonBlock deleteButtonBlock;
2)、在返回页面跳转的时候传值,所以在cell.m文件里面的按钮的点击事件里面调用或赋值
-(void)Click{
if (_deleteButtonBlock) {
_deleteButtonBlock(self.name);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
3)、在第一个视图控制器中的页面跳转中给Block赋值调用它的setter方法
-(void)Click{
ViewController2 *vc=[[ViewController2 alloc]init];
//用到self的地方,强引用,防止内存泄漏
__block ViewController* bself = self;
vc.deleteButtonBlock = ^(NSString *name) {
bself.lable.text=name;
};
[self presentViewController:vc animated:YES completion:nil];
}
三、心得:
- 赋值最好使用
if (_kanBlock) {
self.kanBlock();
}