完全剪斷導(dǎo)航欄跳轉(zhuǎn)時ViewController之間的耦合

#import "FirstViewController.h"

FirstViewController *controller = [[FirstViewController alloc] init]; 
[self.navigationController pushViewController:controller animated:YES];

上面這段代碼是ios開發(fā)中很常見的一段代碼扣癣,但是這平常無奇的代碼卻有一個隱患,這個隱患在隨項目不斷擴展會越來越嚴重憨降。那就是
ViewController之間是存在耦合的父虑,想要跳轉(zhuǎn)目標ViewController,則必須引入對應(yīng)的類頭文件授药。更有甚者士嚎,在ViewController的.h文件中暴露屬性和方法,簡直無法直視悔叽。這次主要解決的問題就是徹底剪斷ViewController之間的耦合莱衩,清理ViewController的.h文件中暴露的內(nèi)容,還一個清爽的ViewController娇澎。

流程圖
導(dǎo)航欄流程圖.png
自定義全局導(dǎo)航欄
  • 初始化導(dǎo)航欄
    因為需要統(tǒng)一對目標ViewController初始化笨蚁,增刪改查等操作,需要自定義一個全局導(dǎo)航欄趟庄,為了方便處理括细,把導(dǎo)航欄做成單例。
// WBNavigationController.h
@interface WBNavigationController : UINavigationController
+ (instancetype)sharedInstance;
@end

// WBNavigationController.m
@interface WBNavigationController ()
// 保存所有注冊的ViewController的URL與類名
@property (nonatomic, strong) NSMutableDictionary *registerVCCls;
@end

@implementation WBNavigationController
+ (instancetype)sharedInstance {
    static WBNavigationController *navigationController = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        navigationController = [[WBNavigationController alloc] init];
    });
    return navigationController;
}
  • 導(dǎo)航欄操作
// WBNavigationController.m

// 注冊一個ViewController到導(dǎo)航欄
+ (void)registerWithUrl:(NSString *)url viewControllerClass:(Class)cls {
    [WBNavigationController sharedInstance].registerVCCls[url] = cls;
}
// 移除導(dǎo)航欄中一個ViewController的實例
+ (void)removeViewControllerWithUrl:(NSString *)url {
    NSMutableArray *viewControllers = [[WBNavigationController sharedInstance].viewControllers mutableCopy];
    UIViewController *targetVC = [[self class] findViewControllerIfExistWithUrl:url];
    if ( [viewControllers containsObject:targetVC] ) {
        [viewControllers removeObject:targetVC];
    }
    
    [WBNavigationController sharedInstance].viewControllers = viewControllers;
}
// 根據(jù)url查找ViewController的類名
+ (Class)findViewControllerClassWithUrl:(NSString *)url {

    return [WBNavigationController sharedInstance].registerVCCls[url];
}

// 導(dǎo)航欄中是否存在ViewController的實例
+ (BOOL)existViewControllerWithUrl:(NSString *)url {

    NSMutableArray *viewControllers = [[WBNavigationController sharedInstance].viewControllers mutableCopy];
    UIViewController *targetVC = [[self class] findViewControllerIfExistWithUrl:url];
    
    if ( [viewControllers containsObject:targetVC] ) {
        return YES;
    }
    
    return NO;
}

// 獲取導(dǎo)航欄中的ViewController的實例
+ (UIViewController *)findViewControllerIfExistWithUrl:(NSString *)url {

    Class vcClassName = [[self class] findViewControllerClassWithUrl:url];
    for (UIViewController *vc in [WBNavigationController sharedInstance].viewControllers) {
        if ( vcClassName == vc.class ) {
            return vc;
        }
    }
    
    return nil;
}
// 取消注冊
+ (void)deregisterUrl:(NSString *)url {
    
    [[WBNavigationController sharedInstance].registerVCCls removeObjectForKey:url];
}
ViewController類別

為了方便調(diào)用戚啥,給ViewController添加一個類別用于調(diào)用導(dǎo)航欄的操作奋单。

  • 初始化目標ViewController
    .h頭文件中暴露屬性與方法無非就是傳遞參數(shù),與適時的回調(diào)猫十。為了清除這些览濒,給每個目標ViewController添加參數(shù)傳遞與回調(diào)block。
#import "UIViewController+URL.h"

@interface UIViewController (URL)
// 給目標ViewController傳遞的參數(shù)
@property (nonatomic, strong) id             wb_params;
// 給目標ViewController的回調(diào)
@property (nonatomic, copy) WBReplyAction    wb_replyAction; 

// 初始化目標ViewController
- (instancetype)initWithParams:(id)params;
- (instancetype)initWithParams:(id)params replyAction:(WBReplyAction)replyAction;
  • 封裝導(dǎo)航欄操作
    封裝導(dǎo)航欄常用操作拖云,push贷笛,pop,以及目標ViewController的present&&dismiss操作宙项。以下以push為例昨忆。
#import "UIViewController+URL.h"
// push操作
- (void)wb_pushViewController:(WBParams)params;
- (void)wb_pushSimpleViewController:(NSString *)url;

- (void)wb_popViewController;
- (void)wb_popViewControllerAnimate:(BOOL)animated;
- (void)wb_popToRootViewControllerAnimated:(BOOL)animated;
- (void)wb_popToViewControllerWithUrl:(NSString *)url animated:(BOOL)animated;

- (void)wb_presentViewController:(WBParams)params;
- (void)wb_presentSimpleViewController:(NSString *)url;

- (void)wb_dismissSimpleViewController;
- (void)wb_dismissViewControllerAnimated:(BOOL)animated completion:(WBCompleteAction)completion;

#import "UIViewController+URL.m"

- (void)wb_pushSimpleViewController:(NSString *)url {

    [self wb_pushViewController:^(WBNode *node) {
        node.url = url;
    }];
}

- (void)wb_pushViewController:(WBParams)params {

    WBNode *node = [self setupNode:params];

    Class vcClass = [WBNavigationController findViewControllerClassWithUrl:node.url];
    if ( !vcClass ) {
        NSLog(@"URL:%@ not register", node.url);
    }
    UIViewController *controller = [[vcClass alloc] initWithParams:node.params replyAction:node.replyAction];
    
    [[WBNavigationController sharedInstance] pushViewController:controller animated:node.animate];
}
  • 測試調(diào)用
  1. 在AppDelegate設(shè)置window的rootViewController為全局導(dǎo)航欄。
FirstViewController *rootViewController = [[FirstViewController alloc] init];
[[WBNavigationController sharedInstance] pushViewController:rootViewController animated:NO];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [WBNavigationController sharedInstance];
[self.window makeKeyAndVisible];

2.注冊ViewController到全局導(dǎo)航欄杉允。

// 定義快速注冊viewcontroller的宏
#undef  WB_IMPLEMENT_LOAD
#define WB_IMPLEMENT_LOAD( url ) \
+ (void)load { \
@autoreleasepool { \
    [WBNavigationController registerWithUrl:url viewControllerClass:[self class]]; \
} \
}

#import "SecondViewController.h"
// 注冊
@implementation SecondViewController
WB_IMPLEMENT_LOAD(URL_SECOND_VC)

3.跳轉(zhuǎn)調(diào)用

#import "FirstViewController.h" // 不需要引用目標ViewController邑贴,此處是主調(diào)方的席里。
// 簡單調(diào)用,不需要傳遞參數(shù)與回調(diào)
[self wb_pushSimpleViewController: URL_SECOND_VC];

// 完全調(diào)用
[self wb_pushViewController:^(WBNode *node) {
      node.url = URL_SECOND_VC;
//      node.animate = NO;
      node.params = @{@"params": @"push data"};// 參數(shù)傳遞
      node.replyAction = ^(id result) {  // 回調(diào)
            NSLog(@"result >> %@", result[@"result"]);
        };
}];

#import "SecondViewController.h"
// 獲取從前頁面?zhèn)鬟f來的參數(shù)
if( self.wb_params ) NSLog(@"push get params >> %@", self.wb_params[@"params"]);

// 觸發(fā)前頁面的回調(diào)
if ( self.wb_replyAction ) {
        self.wb_replyAction(@{@"result": @"pop return data"});
    }

至此已完成了解決UIViewController之間的耦合問題÷<荩現(xiàn)在我們來對比一下前后代碼對照:

#import "FirstViewController.h"

// 優(yōu)化前
FirstViewController *controller = [[FirstViewController alloc] init]; 
[self.navigationController pushViewController:controller animated:YES];

// 優(yōu)化后
[self wb_pushSimpleViewController: URL_FIRST_VC];

// 優(yōu)化后所有ViewController的頭文件應(yīng)該都是這樣奖磁,清爽無比。
@interface FirstViewController : UIViewController

@end

以上因為跳轉(zhuǎn)ViewController間已不存在任何依賴繁疤,調(diào)用簡潔清晰咖为,更有利于項目的模塊化。demo已上傳至github,有任何錯誤與建議可以評論指出稠腊。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末躁染,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子架忌,更是在濱河造成了極大的恐慌吞彤,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叹放,死亡現(xiàn)場離奇詭異饰恕,居然都是意外死亡,警方通過查閱死者的電腦和手機井仰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門埋嵌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人俱恶,你說我怎么就攤上這事雹嗦。” “怎么了合是?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵了罪,是天一觀的道長。 經(jīng)常有香客問我端仰,道長,這世上最難降的妖魔是什么田藐? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任荔烧,我火速辦了婚禮,結(jié)果婚禮上汽久,老公的妹妹穿的比我還像新娘鹤竭。我一直安慰自己,他們只是感情好景醇,可當我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布臀稚。 她就那樣靜靜地躺著,像睡著了一般三痰。 火紅的嫁衣襯著肌膚如雪吧寺。 梳的紋絲不亂的頭發(fā)上窜管,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天,我揣著相機與錄音稚机,去河邊找鬼幕帆。 笑死,一個胖子當著我的面吹牛赖条,可吹牛的內(nèi)容都是我干的失乾。 我是一名探鬼主播,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼纬乍,長吁一口氣:“原來是場噩夢啊……” “哼碱茁!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起仿贬,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤纽竣,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后诅蝶,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體退个,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年调炬,在試婚紗的時候發(fā)現(xiàn)自己被綠了语盈。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡缰泡,死狀恐怖刀荒,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情棘钞,我是刑警寧澤缠借,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站宜猜,受9級特大地震影響泼返,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜姨拥,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一绅喉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧叫乌,春花似錦柴罐、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春似芝,著一層夾襖步出監(jiān)牢的瞬間那婉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工国觉, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留吧恃,地道東北人。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓麻诀,卻偏偏與公主長得像痕寓,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子蝇闭,可洞房花燭夜當晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容