小伙伴們,我又回來了,最近一直很忙碌,今天把手里的項目需求寫完了,有點時間給大家整理一下模塊化調用的第一節(jié),講一下routable-ios的簡單原理和他的好處
隨著項目的業(yè)務模塊越來越多,代碼越來越零碎,協(xié)同開發(fā)的時候相互頁面調用會越來越臃腫,而且呈現(xiàn)碎片化,沒有辦法統(tǒng)一去管理,所以routable-ios出現(xiàn)了,看一下它的簡單使用
1.注冊
//在didFinishLaunchingWithOptions里注冊
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[[Routable sharedRouter] map:@"user/:id" toController:[ViewController class]];
[[Routable sharedRouter] map:@"sec/:str" toController:[SecViewController class]];
[[Routable sharedRouter] setNavigationController:nav];
[self.window setRootViewController:nav];
[self.window makeKeyAndVisible];
return YES;
}
[[Routable sharedRouter] map:@"user/:id" toController:[ViewController class]];里的map是和后臺約定好的規(guī)則,user是host,后面接著你需要的參數(shù),后臺返回給你字符串,就可以自己去跳轉和處理需要的參數(shù)了
查看源代碼,他其實通過map值經(jīng)過UPRouterOptions類的轉換,把他當做key映射到字典里,value是控制器
//源代碼
- (void)map:(NSString *)format toController:(Class)controllerClass withOptions:(UPRouterOptions *)options {
if (!format) {
@throw [NSException exceptionWithName:@"RouteNotProvided"
reason:@"Route #format is not initialized"
userInfo:nil];
return;
}
if (!options) {
options = [UPRouterOptions routerOptions];
}
options.openClass = controllerClass;
[self.routes setObject:options forKey:format];
}
2.調用
//點擊跳轉的時候條用一下方法,傳一下參數(shù)
-(void)click{
[[Routable sharedRouter] open:@"sec/12" animated:YES extraParams:@{@"temp":@"1"}];
}
源代碼太多了,我就不貼出來了,首先根據(jù)傳入的open字符串通過- (RouterParams *)routerParamsForUrl:(NSString *)url extraParams: (NSDictionary *)extraParams;這個方法來把傳入的參數(shù)格式化,賦值給UPRouterOptions,通過UPRouterOptions相對應的字典里的key值,查找到需要跳轉的控制器
3.處理參數(shù)
//在SecViewController 里下一下router初始化方法里接一下傳過來的字典,就ok了
- (id)initWithRouterParams:(NSDictionary *)params {
if ((self = [self initWithNibName:nil bundle:nil])) {
}
return self;
}
routable-ios有他的好處,在view上也可以跳轉,點擊推送也可以直接在appdelagate跳轉Controller,而且注冊的時候可以寫在一個基類里同意管理
同樣他也有缺點,傳入的參數(shù)不支持url格式,不過這個我覺得利大于弊,可以一用
demo地址