1、自定义一个继承于UIButton的按钮
2、声明block:
返回值类型 函数名 参数列表
typedef void (^ButtonBlock)(UIButton *);
@interface MyButton : UIButton
@property(nonatomic,copy)ButtonBlock block;
- (void)addTapBlock:(ButtonBlock)block;
@end
3、重写button的点击事件的方法
- (void)addTapBlock:(ButtonBlock)block
{
_block = block;
[self addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonAction:(UIButton *)button
{
_block(button);
}
4、在视图控制器中调用button的block方法
[button addTapBlock:^(UIButton *button){
NSLog(@"按钮被点击了");
}];