route跳轉(zhuǎn)實(shí)際上是用一個(gè)url鏈接實(shí)現(xiàn)APP的頁(yè)面跳轉(zhuǎn)功能杖爽,解析url路徑敲董,獲取要跳轉(zhuǎn)的控制器紫皇、參數(shù)。
//跳轉(zhuǎn)
+ (void)routerPush:(NSString *)urlStr {
NSDictionary *dic = [ccRouter queryItems:[NSURL URLWithString:urlStr]];
NSString *class =[NSString stringWithFormat:@"%@", dic[@"className"]];
const char *className = [class cStringUsingEncoding:NSASCIIStringEncoding];
// 從一個(gè)字串返回一個(gè)類
Class nClass = objc_getClass(className);
//創(chuàng)建對(duì)象腋寨、參數(shù)賦值
id instance = [[nClass alloc] init];
[ccRouter setParamWith:dic instance:instance];
UIViewController* currentVc = [ccRouter currentVc];
if (currentVc.navigationController) {
[currentVc.navigationController pushViewController:instance animated:YES];
}else{
[currentVc presentViewController:instance animated:YES completion:nil];
}
}
//當(dāng)前顯示的控制器
+ (UIViewController *)currentVc {
UIViewController *topC = [ccRouter topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
while (topC.presentedViewController) {
topC = [self topViewController:topC.presentedViewController];
}
return topC;
}
+ (UIViewController *)topViewController:(UIViewController *)controller {
if ([controller isKindOfClass:[UINavigationController class]]) {
return [ccRouter topViewController:[(UINavigationController *)controller topViewController]];
} else if ([controller isKindOfClass:[UITabBarController class]]) {
return [ccRouter topViewController:[(UITabBarController *)controller selectedViewController]];
} else {
return controller;
}
}