写了个UIButton的分类
//个人写代码有整理强迫症,不喜欢uibutton每次都需要重新写个函数,就写了个分类!
//一些有重复调用必要的函数还是不要用此分类的好
-
@interface UIButton (Block)
-
/*
-
* handleControlEvent:withBlock:
-
* 使用block处理button事件
-
* 入参:event 触发类型 例: UIControlEventTouchUpInside
-
* block 满足触发条件后的事件 例:^{}
-
* 注:最后入参有效,同时只能保存一个block触发事件
-
*/
-
- (void)handleControlEvent:(UIControlEvents)event withBlock:(dispatch_block_t)block;
-
@end
//.m文件开始
-
-
@implementation UIButton (Block)
-
- (void)handleControlEvent:(UIControlEvents)event withBlock:(dispatch_block_t)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
-
{
-
dispatch_block_t block=objc_getAssociatedObject(self, &"myBlock");
-
if(block)
-
{
-
block();
-
}
-
}
-
1
//手上还有个UIAlertView的分类,效果基本等同.有需要的自行更改就是了,那也是极好的
//最后吐槽一下CSDN的排版,彻底不会用编辑器了
//最后推荐一下念茜的博客: http://blog.csdn.net/yiyaaixuexi