MGLRouter主要是做路由跳轉(zhuǎn)伊磺,可以減少項(xiàng)目中不同模塊之間的耦合昙篙,更利于代碼分離柳弄。
常見(jiàn)用法是先注冊(cè)矛渴,在使用秆撮。
可以注冊(cè)一個(gè)單例绢彤,在單例中把后續(xù)要使用到的跳轉(zhuǎn)controller之類注冊(cè)進(jìn)去描馅,然后在回調(diào)中做好對(duì)參數(shù)的分離营勤,獲取富弦。然后實(shí)現(xiàn)跳轉(zhuǎn)的代碼邏輯沟娱。
[MGJRouter registerURLPattern:@"test://detail"toHandler:^(NSDictionary*routerParameters) {
? ? ? ? NSString*detailId = routerParameters[@"detailId"];
? ? ? ? DetailVC *detailVC= [[DetailVCalloc] initWithDetailId:detailId];
? ? ? ? [[Appmanager topViewController].navigationController pushViewController:detailVC animated:YES];
? ? }];
或者實(shí)現(xiàn)一個(gè)單獨(dú)的類,在類的+ (void)load方法中實(shí)現(xiàn)上面邏輯腕柜,這樣在其他地方就可以直接使用就行济似,不用其他額外代碼。
使用時(shí)直接調(diào)用URL盏缤,然后傳入相關(guān)的參數(shù)就可以了砰蠢。
[MGJRouter openURL:@"test://detail" withUserInfo:@{@"detailId" : @"123"} completion:nil];