Method Swizzling 的一個(gè)使用場(chǎng)景

很早就看過(guò)一些關(guān)于 Method Swizzling 的博客,看完后一直沒(méi)有一個(gè)恰當(dāng)?shù)氖褂脠?chǎng)景能操練一下。最近寫(xiě)一個(gè) Demo 的時(shí)候發(fā)現(xiàn)要在很多控制器里寫(xiě)導(dǎo)航欄的返回按鈕,本來(lái)是復(fù)制一下或者繼承一下就行的。但是復(fù)制這種做法挺蠢的,繼承會(huì)讓代碼耦合性增加。這個(gè)時(shí)候我就突然的想到了 Method Swizzling,然后做了一個(gè)嘗試。

創(chuàng)建一個(gè) UIViewController 的分類(lèi),引入#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];
    
    //在這里判斷哪個(gè)控制器不需要返回按鈕
    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

這樣就實(shí)現(xiàn)了,下篇見(jiàn)~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 2,041評(píng)論 0 9
  • 文中的實(shí)驗(yàn)代碼我放在了這個(gè)項(xiàng)目中。 以下內(nèi)容是我通過(guò)整理[這篇博客] (http://yulingtianxia....
    茗涙閱讀 1,026評(píng)論 0 6
  • 轉(zhuǎn)載:http://yulingtianxia.com/blog/2014/11/05/objective-c-r...
    F麥子閱讀 828評(píng)論 0 2
  • 本文轉(zhuǎn)自:楊蕭玉博客 本文詳細(xì)整理了 Cocoa 的 Runtime 系統(tǒng)的知識(shí),它使得 Objective-C ...
    oneofai閱讀 247評(píng)論 0 0
  • 繼上篇安裝使用教程,在這里寫(xiě)一下可能會(huì)遇到的問(wèn)題:這個(gè)也是最坑的地方,有些我有遇到,有些沒(méi)有,在這里整理了下,心累...
    你的小福蝶閱讀 796評(píng)論 0 1

友情鏈接更多精彩內(nèi)容