Method Swizzling 的一个使用场景

很早就看过一些关于 Method Swizzling 的博客,看完后一直没有一个恰当的使用场景能操练一下。最近写一个 Demo 的时候发现要在很多控制器里写导航栏的返回按钮,本来是复制一下或者继承一下就行的。但是复制这种做法挺蠢的,继承会让代码耦合性增加。这个时候我就突然的想到了 Method Swizzling,然后做了一个尝试。

创建一个 UIViewController 的分类,引入#import 头文件。基本代码如下:

@implementation UIViewController (BackButton)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
    
        SEL originalSelector = @selector(viewWillAppear:);
        SEL swizzledSelector = @selector(swizzled_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);
        }
    });
}

#pragma mark - Method Swizzling
- (void)swizzled_viewWillAppear:(BOOL)animated {
    [self swizzled_viewWillAppear:animated];
    
    //在这里判断哪个控制器不需要返回按钮
    if (![self isMemberOfClass:NSClassFromString(@"ViewController")]) {
        UIImage *image = [UIImage imageNamed:@"goBack_blue.png"];
        UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
        [leftButton setImage:image forState:UIControlStateNormal];
        [leftButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
        self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    }

    NSLog(@"swizzled_viewWillAppear");
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
}

@end

这样就实现了,下篇见~

你可能感兴趣的:(Method Swizzling 的一个使用场景)