
就走两步路,有必要开飞机去吗?有很多傻逼,想把UIButton的点击事件和创建放到一块,导入了RAC。导入一个类库,一个项目就用到了三五个地方,大材小用,且ipa瞬间增大,何必呢?
效果

使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#import "UIControl+Ext.h" @interface () @end @implementation - (void)viewDidLoad { [super viewDidLoad]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [self.view addSubview:btn]; btn.frame = CGRectMake(0, 0, 200, 200); btn.center = self.view.center; btn.backgroundColor = [UIColor greenColor]; [btn clickWithHandler:^{ NSLog(@"点击了按钮"); }]; } @end
|
UIControl+Ext.h
1 2 3 4 5 6 7 8 9 10
|
#import <UIKit/UIKit.h> typedef void(^HH_ClickBlock)(); @interface UIControl (Ext) - (void)clickWithHandler:(HH_ClickBlock)block; @end
|
UIControl+Ext.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#import "UIControl+Ext.h" #import <objc/runtime.h> @implementation UIControl (Ext) - (void)clickWithHandler:(HH_ClickBlock)block { objc_setAssociatedObject(self, "hhclickBlock", block, OBJC_ASSOCIATION_COPY); [self addTarget:self action:@selector(handleAction:) forControlEvents:UIControlEventTouchUpInside]; } - (void)handleAction:(id)sender { HH_ClickBlock block = objc_getAssociatedObject(self, "hhclickBlock"); if (block) { block(); } } @end
|
近期评论