很早就看過一些關(guān)于 Method Swizzling 的博客仔涩,看完后一直沒有一個恰當(dāng)?shù)氖褂脠鼍澳懿倬氁幌滤魃汀W罱鼘懸粋€ Demo 的時候發(fā)現(xiàn)要在很多控制器里寫導(dǎo)航欄的返回按鈕,本來是復(fù)制一下或者繼承一下就行的篇梭。但是復(fù)制這種做法挺蠢的蔼两,繼承會讓代碼耦合性增加。這個時候我就突然的想到了 Method Swizzling赔桌,然后做了一個嘗試供炎。
創(chuàng)建一個 UIViewController 的分類,引入#import <objc/runtime.h>
頭文件疾党∫艚耄基本代碼如下:
@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
這樣就實現(xiàn)了,下篇見~