为 iOS 开发提供方便的一些小工具的集合
感兴趣的可以加QQ群交流:70835656 (JC开源项目交流)
#import "JCAssistant.h"
在 UIControl 中添加监控事件,如为 UIButton 添加点击事件,我们会这样写:
//添加UIControlEventTouchUpInside事件
[button addTarget:self action:@selector(buttonAction:) forControlEvents: UIControlEventTouchUpInside];
//事件处理方法
- (void)buttonAction:(UIButton*)sender{
//TO-DO
}
使用 JCAssistant 可以这样写:
#import <JCAssistant/JCAssistant.h>
或
#import <JCAssistant/UIControl+JC_Extension.h>
[button jc_actionWithEvent:UIControlEventTouchUpInside usingBlock:^{
NSLog(@"我被点击了");
}];
使用 JCAssistant 为 UIControl 添加的事件监听可以通过 jc_removeActionWithEvent/jc_removeAllAction
进行移除监听
//移除UIControlEventTouchUpInside
[button jc_removeActionWithEvent:UIControlEventTouchUpInside];
//移除所有监听
[button jc_removeAllAction];
如果使用了 JCAssistant 添加了多个监听事件,如:
[button jc_actionWithEvent:UIControlEventTouchUpInside usingBlock:^{
NSLog(@"---执行--1--");
}];
[button jc_actionWithEvent:UIControlEventTouchUpOutside usingBlock:^{
NSLog(@"---执行--2--");
}];
可以这样移除:
//分多次移除
[button jc_removeActionWithEvent:UIControlEventTouchUpInside];
[button jc_removeActionWithEvent:UIControlEventTouchUpOutside];
//一次性移除
[button jc_removeActionWithEvent:UIControlEventTouchUpOutside | UIControlEventTouchUpInside];