kingleekong
1/2/2018 - 3:13 AM

交换方法.m

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class cls = [self class];
        SEL selOrigin = @selector(pushViewController:animated:);
        SEL selReplace = @selector(c_pushViewController:animated:);
        Method mOrigin = class_getInstanceMethod(cls, selOrigin);
        Method mReplace = class_getInstanceMethod(cls, selReplace);
        BOOL isSuccess = class_addMethod(cls, selReplace, class_getMethodImplementation(cls, selReplace), method_getTypeEncoding(mReplace));
        if (isSuccess)
        {
            //添加成功
            class_replaceMethod(cls, selReplace, class_getMethodImplementation(cls, selOrigin), method_getTypeEncoding(mOrigin));
        }
        else
        {
            //添加失败
            method_exchangeImplementations(mOrigin, mReplace);
        }
    });
}