本文通過一個示例來介紹runtime的攔截替換方法的使用,該魔法主要用於給系統方法添加新的功能,示例爲系統按鈕的點擊事件增加了一個計算點擊次數的方法。
#import "UIButton+Count.h"
#import <objc/runtime.h>
#import "ZTools.h"
@implementation UIButton (Count)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class selfClass = [self class];
//原來的方法
SEL oriSEL = @selector(sendAction:to:forEvent:);
Method oriMethod = class_getInstanceMethod(selfClass, oriSEL);
//現在的方法
SEL nowSEL = @selector(addBtnClickCount:to:forEvent:);
Method nowMethod = class_getInstanceMethod(selfClass, nowSEL);
//給原來的方法添加實現,防止原來的方法沒有實現引起奔潰
BOOL addSupC = class_addMethod(selfClass, oriSEL, method_getImplementation(nowMethod), method_getTypeEncoding(oriMethod));
if (addSupC) {
//添加成功,則用現在的方法實現代替原來的方法
class_replaceMethod(selfClass, nowSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
}else{
//添加失敗,證明原來的方法有實現,直接交換這兩個方法
method_exchangeImplementations(oriMethod, nowMethod);
}
});
}
//現在的方法
-(void)addBtnClickCount:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
[[ZTools shareInstance] countClicks];
[super sendAction:action to:target forEvent:event];
}
@end
#import <Foundation/Foundation.h>
@interface ZTools : NSObject
@property (nonatomic,assign) NSInteger count;
+(instancetype)shareInstance;
-(void)countClicks;
@end
#import "ZTools.h"
@implementation ZTools
static id _instance;
+(instancetype)shareInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[ZTools alloc] init];
});
return _instance;
}
-(void)countClicks
{
_count += 1;
NSLog(@"點擊次數:%ld",_count);
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 150, 100, 50)];
[btn setTitle:@"btn" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:btn];
UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 250, 100, 50)];
[btn1 setTitle:@"btn1" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn1 setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:btn1];
}
-(void)btnClicked:(UIButton *)sender
{
NSLog(@"btn clicked...");
}