swizzle

2017-02-07  本文已影响10人  疾风追马
#import "UIViewController+Tracking.h"
#import <objc/runtime.h>

@implementation UIViewController (Tracking)

+ (void)load {
    
    NSString *className = NSStringFromClass(self.class);
    NSLog(@"className_______%@",className);
    
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        Class class = [self class];
        
        SEL originalSelector = @selector(viewWillAppear:);
        SEL swizzledSelector = @selector(sl_viewWillAppear:);
        
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        
        BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
        
        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
    
}

- (void)sl_viewWillAppear:(BOOL)animated{
    
    NSLog(@"sl_viewWillAppear_______%@",self);
    [self sl_viewWillAppear:animated];
}
上一篇下一篇

猜你喜欢

热点阅读