iOS runtime(二)

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值