使用組件化是為了解耦處理,多個(gè)模塊之間通過協(xié)議進(jìn)行交互。而負(fù)責(zé)解析協(xié)議,找到目的控制器,或者是返回對(duì)象給調(diào)用者的這個(gè)組件就是路由組件。本文講解如何使用核心的50行代碼實(shí)現(xiàn)一個(gè)路由組件。
組件化和路由
之前看過挺多的關(guān)于路由管理、路由處理的文章,常常會(huì)和組件化出現(xiàn)在一起,一開始不知道為何路由和組件化出現(xiàn)在一起,后來公司的項(xiàng)目中使用了路由組件(他本身也是一個(gè)組件,確切的說是一個(gè)中間人或者中介者),才突然想明白了,原來如此。
使用組件化是為了解耦處理,多個(gè)模塊之間通過協(xié)議進(jìn)行交互。而負(fù)責(zé)解析協(xié)議,找到目的控制器,或者是返回對(duì)象給調(diào)用者的這個(gè)組件就是路由組件。
路由組件的職責(zé)主要是:
注冊者傳遞path和path對(duì)應(yīng)的block,block的具體實(shí)現(xiàn)又注冊者自己處理
調(diào)用者最簡單可以傳遞一個(gè)path給路由組件發(fā)起調(diào)用,路由組件會(huì)把具體的處理轉(zhuǎn)發(fā)給注冊者,理論上是可以任意的操作,包括頁面跳轉(zhuǎn)、彈窗提示、返回一個(gè)值給調(diào)用者等
下面會(huì)會(huì)在以上分析的基礎(chǔ)上實(shí)現(xiàn)一個(gè)簡單的路由組件,對(duì)應(yīng)的代碼可以在YTRouterDemo這里找到
路由的實(shí)現(xiàn)
路由的實(shí)現(xiàn)包括兩部分:路由注冊實(shí)現(xiàn)以及路由使用實(shí)現(xiàn)
路由注冊實(shí)現(xiàn)
路由注冊實(shí)現(xiàn)時(shí)序圖:
如上圖所示,步驟很簡單:
以上步驟對(duì)應(yīng)的代碼如下:
- (void)registerPath:(NSString *)path actionBlock:(RouterActionBlock)actionBlock { YTRouterActionObject *actionObject = [YTRouterActionObject new]; actionObject.path = path; actionObject.actionBlock = actionBlock; NSMutableDictionary *subRouter = [self subRouterMapWithPath:path]; subRouter[YTRouterActionObjectKey] = actionObject;}- (NSMutableDictionary *)subRouterMapWithPath:(NSString *)path { NSArray *components = [path componentsSeparatedByString:@"/"]; NSMutableDictionary *subRouter = self.routerMap; for (NSString *component in components) { if (component.length == 0) { continue; } if (!subRouter[component]) { subRouter[component] = [NSMutableDictionary new]; } subRouter = subRouter[component]; } return subRouter;}在Demo中注冊的幾個(gè)路由最終的配置如下,比如home/messagelist對(duì)應(yīng)的路由配置保存在<YTRouterActionObject: 0x6040000365e0>對(duì)象中
Printing description of self->_routerMap:{ home = { "_" = "<YTRouterActionObject: 0x60c00003b040>"; messagelist = { "_" = "<YTRouterActionObject: 0x6040000365e0>"; detail = { "_" = "<YTRouterActionObject: 0x600000038ec0>"; }; getmessage = { "_" = "<YTRouterActionObject: 0x600000038e80>"; }; }; };}路由使用實(shí)現(xiàn)
路由使用實(shí)現(xiàn)時(shí)序圖:
如上圖所示,步驟很簡單:
從注冊的配置中找到匹配的YTRouterActionObject對(duì)象
執(zhí)行YTRouterActionObject對(duì)象的actionBlock,會(huì)傳遞一個(gè)YTRouterActionCallbackObject對(duì)象,如果調(diào)用者需要的是返回值,可以使用YTRouterActionCallbackObject對(duì)象的actionCallbackBlock傳遞一個(gè)返回值,這個(gè)actionBlock是又業(yè)務(wù)方的注冊者實(shí)現(xiàn)的
以上步驟對(duì)應(yīng)的代碼如下:
- (BOOL)runWithActionCallbackObject:(YTRouterActionCallbackObject *)actionCallbackObject { // 判斷是否支持scheme if (![self canAcceptScheme:actionCallbackObject.uri.scheme]) { return NO; } // 獲取path對(duì)應(yīng)的ActionObject YTRouterActionObject *actionObject = [self actionObjectWithPath:actionCallbackObject.uri.path]; // 執(zhí)行Path注冊的對(duì)應(yīng)Block !actionObject.actionBlock ?: actionObject.actionBlock(actionCallbackObject); return YES;}- (YTRouterActionObject *)actionObjectWithPath:(NSString *)path { NSMutableDictionary *subRouter = [self subRouterMapWithPath:path]; return subRouter[YTRouterActionObjectKey];}客戶端的使用
以上講到了核心的路由注冊實(shí)現(xiàn)和路由使用實(shí)現(xiàn),總共代碼還沒有50行,所以還是很簡單的,接下來會(huì)講下客戶端的使用步驟,包括
客戶端注冊者注冊
注冊的時(shí)機(jī)需要比較找,考慮到集成的方便,選擇在load方法中處理路由注冊,如下代碼所示,添加了幾個(gè)測試的路由,分兩種情況來說明下使用
1、不需要返回值
如下注冊"home/messagelist"的是一個(gè)頁面跳轉(zhuǎn)的路由,actionBlock的參數(shù)是一個(gè)YTRouterActionCallbackObject對(duì)象,可以從YTRouterActionCallbackObject對(duì)象或者到參數(shù),關(guān)于如何傳遞值,會(huì)在下面的客戶端調(diào)用者使用這里講到。然后在actionBlock處理目的頁面的初始化、參數(shù)設(shè)置等步驟,然后執(zhí)行頁面跳轉(zhuǎn)。
2、需要返回值
如下注冊"home/messagelist/getmessage"的是一個(gè)提供返回值的路由,同樣也可以從YTRouterActionCallbackObject對(duì)象獲取參數(shù),另外YTRouterActionCallbackObject對(duì)象還有一個(gè)actionCallbackBlock屬性是專門處理返回參數(shù)給調(diào)用者的,如下的代碼只是簡單返回一個(gè)字符串,在更加具體的業(yè)務(wù)場景中,這里會(huì)設(shè)置接口調(diào)用、數(shù)據(jù)庫查詢等任務(wù),最后把結(jié)果返回。
@implementation ModuleAUriRegister+ (void)load { [[YTRouterManager sharedRouterManager] registerPath:@"home/messagelist" actionBlock:^(YTRouterActionCallbackObject *callbackObject) { MessageListViewController *messageListVC = [MessageListViewController new]; NSString *title = callbackObject.uri.params[@"title"]; messageListVC.title = title; [[UIViewController yt_currentViewControlloer].navigationController pushViewController:messageListVC animated:YES];; }]; [[YTRouterManager sharedRouterManager] registerPath:@"home/" actionBlock:^(YTRouterActionCallbackObject *callbackObject) { }]; [[YTRouterManager sharedRouterManager] registerPath:@"home/messagelist/detail" actionBlock:^(YTRouterActionCallbackObject *callbackObject) { }]; [[YTRouterManager sharedRouterManager] registerPath:@"home/messagelist/getmessage" actionBlock:^(YTRouterActionCallbackObject *callbackObject) { // 內(nèi)容回調(diào) !callbackObject.actionCallbackBlock ?: callbackObject.actionCallbackBlock(@"message content text demo"); }];}@end客戶端調(diào)用者使用
1、簡單的path跳轉(zhuǎn)調(diào)用
使用YTRouterManager單例對(duì)象的runWithPath方法,傳遞一個(gè)注冊的path參數(shù)完成跳轉(zhuǎn)。
[self addActionWithTitle:@"Router頁面跳轉(zhuǎn)" detailText:@"home/messagelist" callback:^{ [[YTRouterManager sharedRouterManager] runWithPath:@"home/messagelist"];}];2、使用URL調(diào)用和有URL參數(shù)的調(diào)用
使用YTRouterManager單例對(duì)象的runWithURLString方法,傳遞一個(gè)完整的包含了scheme/path,或者有參數(shù)的會(huì)才有參數(shù)的URL,比如"YTRouter://home/messagelist" 和 "YTRouter://home/messagelist?title=Hello Message" ,路由組件會(huì)解析出里面的scheme、path、params,進(jìn)行scheme過濾處理、path查詢YTRouterActionObject對(duì)象處理、參數(shù)傳遞處理。
[self addActionWithTitle:@"Router使用URL調(diào)用" detailText:@"YTRouter://home/messagelist" callback:^{ [[YTRouterManager sharedRouterManager] runWithURLString:@"YTRouter://home/messagelist"];}];[self addActionWithTitle:@"Router使用帶參數(shù)的URL調(diào)用" detailText:@"YTRouter://home/messagelist?title=Hello Message" callback:^{ [[YTRouterManager sharedRouterManager] runWithURLString:@"YTRouter://home/messagelist?title=Hello Message"];}];效果如下圖所示:
效果圖
3、簡單的path跳轉(zhuǎn)調(diào)用
使用YTRouterManager單例對(duì)象的runWithActionCallbackObject方法,傳遞一個(gè)YTRouterActionCallbackObject類型的參數(shù),設(shè)置YTRouterActionCallbackObject對(duì)象的uri和結(jié)果回調(diào)actionCallbackBlock參數(shù),在actionCallbackBlock中處理返回值。
[self addActionWithTitle:@"Router獲取返回值" detailText:@"home/messagelist/getmessage" callback:^{ __block id message = nil; YTRouterActionCallbackObject *actionCallbackObject = [YTRouterActionCallbackObject new]; actionCallbackObject.uri = [[YTUri alloc] initWithPath:@"home/messagelist/getmessage"]; actionCallbackObject.actionCallbackBlock = ^(id result) { message = result; }; [[YTRouterManager sharedRouterManager] runWithActionCallbackObject:actionCallbackObject]; NSLog(@"message = %@", message);}];一些小想法
總結(jié)
以上所述是小編給大家介紹的iOS使用核心的50行代碼擼一個(gè)路由組件,希望對(duì)大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VEVB武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注