Routable中的類
UPRouterOptions
UPRouter
Routable : UPRouter
RouterParams
Routable繼承自UPRouter,提供了一個單例方法:+ (instancetype)sharedRouter;
保證一個生命周期中只使用同一個Routable
對象琉雳。
Routable是核心類辆沦,實現(xiàn)了注冊、調(diào)用Open跳轉(zhuǎn)頁面等基本功能挤土。
注冊
[[Routable sharedRouter] map:@"user/:id" toController:[ViewController class]];
這里的map是和后臺約定好的規(guī)則琴庵,user是host,后面接著你需要的參數(shù),后臺返回給你字符串迷殿,就可以自己去跳轉(zhuǎn)和處理需要的參數(shù)了儿礼。注冊的格式為host/:param1/:param2
查看源代碼,他其實通過map值經(jīng)過UPRouterOptions
類的轉(zhuǎn)換庆寺,把map的值當(dāng)做key映射到字典self.routes
里蚊夫,value值是對應(yīng)的viewcontroller
。
- (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) {
// like [NSArray array]
options = [UPRouterOptions routerOptions];
}
options.openClass = controllerClass;
[self.routes setObject:options forKey:format];
}
頁面跳轉(zhuǎn)
注冊成功后就可以調(diào)用open
方法進行頁面跳轉(zhuǎn)懦尝。
- (void)open:(NSString *)url animated:(BOOL)animated {
// routerParams 包含ViewController 打開的方式 參數(shù)(openParams)
RouterParams *params = [self routerParamsForUrl:url];
UPRouterOptions *options = params.routerOptions;
// callBack
if (options.callback) {
RouterOpenCallback callback = options.callback;
callback([params controllerParams]);
return;
}
if (!self.navigationController) {
if (_ignoresExceptions) {
return;
}
@throw [NSException exceptionWithName:@"NavigationControllerNotProvided"
reason:@"Router#navigationController has not been set to a UINavigationController instance"
userInfo:nil];
}
UIViewController *controller = [self controllerForRouterParams:params];
if (self.navigationController.presentedViewController) {
[self.navigationController dismissViewControllerAnimated:animated completion:nil];
}
if ([options isModal]) {
if ([controller.class isSubclassOfClass:UINavigationController.class]) {
[self.navigationController presentViewController:controller
animated:animated
completion:nil];
}
else {
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
navigationController.modalPresentationStyle = controller.modalPresentationStyle;
navigationController.modalTransitionStyle = controller.modalTransitionStyle;
[self.navigationController presentViewController:navigationController
animated:animated
completion:nil];
}
}
else if (options.shouldOpenAsRootViewController) {
[self.navigationController setViewControllers:@[controller] animated:animated];
}
else {
[self.navigationController pushViewController:controller animated:animated];
}
}
首先根據(jù)url
獲取對應(yīng)的RouterParams
對象知纷,RouterParams包含RouterOptions
包含openClass
和跳轉(zhuǎn)方式等,openParams
是需要傳遞的參數(shù)陵霉。
然后取到需要跳轉(zhuǎn)的ViewController
琅轧,并且為參數(shù)賦值,設(shè)置跳轉(zhuǎn)形式動畫等撩匕。ViewController
需要實現(xiàn)initWithRouterParams:
方法或者allocWithRouterParams
方法鹰晨,完成參數(shù)的傳遞。然后通過UPRouterOptions
中的參數(shù)判斷跳轉(zhuǎn)方式完成頁面跳轉(zhuǎn)止毕。
如果是callBack
typedef void (^RouterOpenCallBack)(NSDictionary *params)