也不能说是实现吧,,,就是有点类似于RAC里边的写法,不用给btn添加另外的点击事件,就那个add...select...这样子很不友好,来看下代码:
[self.btn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
NSLog(@"2%@",click);
}];
差不多就是这个意思,当然了,此处还是预留了event,RAC直接连这个都没了,具体可以看这里
那么,问题来了,怎么实现呢?
首先,我们对系统的的UIButton做一个category,
然后,写点击事件:
.h
#import
typedef void(^ActionBlock)(void);
@interface UIButton (ClickBlock)
- (void)handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock)block;
@end
.m
#import "UIButton+ClickBlock.h"
#import
@implementation UIButton (ClickBlock)
- (void)handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock)block
{
if(!event)
event=UIControlEventTouchUpInside;
objc_setAssociatedObject(self, &"myBlock", block, OBJC_ASSOCIATION_COPY_NONATOMIC);
[self addTarget:self action:@selector(blockEvent:) forControlEvents:event];
}
-(void)blockEvent:(UIButton *)sender
{
ActionBlock block=objc_getAssociatedObject(self, &"myBlock");
if(block)
{
block();
}
}
此处只是demo哈!
这样子我们就可以按照之前的思路,写出来不分离的btnclick了