swizzle

作者: 疾风追马 | 来源:发表于2017-02-07 15:24 被阅读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];
}

相关文章

网友评论

      本文标题:swizzle

      本文链接:https://www.haomeiwen.com/subject/lcfmittx.html