自定义Button点击方法,可实现button回调

有时候,我们需要在button点击后,取出点击方法执行完后的结果,然而原生的点击方法没有提供回调,那么就需要我们自己进行封装了。下面分两种情况进行了封装,一种是继承,一种是分类。具体看Demo
Jietu20180206-101510.jpg

继承

#import <UIKit/UIKit.h>
@interface BlockButton : UIButton
@property(nonatomic,copy) void(^block)(UIButton*);
-(void)addTapBlock:(void(^)(UIButton *btn))block;
@end


#import "BlockButton.h"
@implementation BlockButton
-(void)addTapBlock:(void(^)(UIButton*))block{
    self.block= block;
    [self addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)click:(UIButton*)btn{
    if(self.block) {
        self.block(btn);
    }
}
@end

调用

#import "BlockButton.h"//引入继承
BlockButton *button0 = [[BlockButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
button0.backgroundColor = [UIColor redColor];
[button0 setTitle:@"测试1" forState:0];
[self.view addSubview:button0];
[button0 addTapBlock:^(UIButton *btn) {
     //在这里调用所需要执行的方法
     [self buttonClick];
}];

-(void)buttonClick{
    NSLog(@"点击方法");
}

分类

#import <UIKit/UIKit.h>
@interface UIButton (Block)
@property(nonatomic ,copy)void(^block)(UIButton*);
-(void)addTapBlock:(void(^)(UIButton*btn))block;
@end


#import "UIButton+Block.h"
#import<objc/runtime.h>
@implementation UIButton (Block)
-(void)setBlock:(void(^)(UIButton*))block
{
    objc_setAssociatedObject(self,@selector(block), block,OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
}
-(void(^)(UIButton*))block
{
    return objc_getAssociatedObject(self,@selector(block));
}
-(void)addTapBlock:(void(^)(UIButton*))block
{
    self.block= block;
    [self addTarget:self action:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
}

-(void)click:(UIButton*)btn
{
    if(self.block) {
        self.block(btn);
    }
}
@end

调用

#import "UIButton+Block.h"//引入分类
UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];
button1.backgroundColor = [UIColor blueColor];
button1.tag = 1001;
[button1 setTitle:@"测试2" forState:0];
[self.view addSubview:button1];
    
__block UIButton *blockSelf = button1;
[button1 addTapBlock:^(UIButton *btn) {
     //在这里调用所需要执行的方法
     [self button:blockSelf callBack:^(NSInteger selectIndex, NSString *title) {
         NSLog(@"tag:%ld    title:%@",(long)selectIndex,title);
     }];
}];

-(void)button:(UIButton *)button callBack:(void (^)(NSInteger selectIndex ,NSString *title))callBack{
    callBack(button.tag,button.currentTitle);
}
这两种方法都可以实现传参和回调,结合具体情况使用吧。
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,462评论 25 708
  • 五年前的初春,我和宋阳在学校门口遇见平安。 我一直不肯相信平安是被人抛弃的,因为它向我跑来的样子实在太可爱了,圆滚...
    果仁小姐阅读 486评论 10 10
  • 12月12日的生日花,冬石楠。 冬石楠是个特殊的植物,有说它像是一种小灌又有说它是一种小乔,挂藤的向上的趋势,极力...
    冬林探花阅读 2,872评论 1 3
  • 职场之中做的每一件事都是我们的名片。在有限的时间里,积累什么,舍弃什么,结果大不相同。摸爬滚打之前,奋斗的正确姿势...
    a8703f283f12阅读 242评论 0 0
  • Joey0_0阅读 312评论 0 0