本文通过一个示例来介绍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...");
}