XMRouter
示例
首先在 application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 注册
[[XMRouter shared] registerWithConfig:[XMConfig new]];
注册路由 两种方式: 全局注册: 实现 XMRouterConfigProtocol 的 globalRouters 方法
//全局注册,在这里注册之后 就不用实现routerkey方法
- (NSDictionary *)globlaRouters {
return @{
@"presentVC":@"XMPresentViewController",
@"paramsExampleVC":[XMParamsExampleViewController class],
};
}
2.在任意类里面实现
+ (NSString *)routerKey {
return @"presentVC";
}
跳转: 1.通过 routerName 跳转
[XMRouter routeWithName:@"presentVC" withParams:@{@"aTitle":@"hello",@"aAge":@"11"} routeType:XMRouterTypePush];
2.通过 routerUrl 跳转
[XMRouter routeWithUrl:@"xmrouter://presentVC?aTitle=hello&aAge=11&XMRouteType=1"];
传参: 1.直接传参: 当参数和属性名相同时自动设置属性
参数映射:实现这个方法后可以根据 params 重新映射到属性
+ (NSDictionary *)ReplacedKeyFromPropertyName {
return @{@"name":@"aName"};
}
2.手动传参: 参数可以自己手动进行转换 参考examle里的 XMParamsExampleViewController
+ (BOOL)shouldCustomTransform:(NSString *)propertyName {
if ([propertyName isEqualToString:@"date"]) {
return YES;
}
if ([propertyName isEqualToString:@"peoples"]) {
return YES;
}
return NO;
}
+ (id)customParam:(id)param toPropertyValueWithPropertyName:(NSString *)propertyName {
if ([propertyName isEqualToString:@"date"]) {
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[param doubleValue]];
return date;
}
if ([propertyName isEqualToString:@"peoples"]) {
return @[param];
}
return nil;
}
手动跳转: 非UIViewController的类需要自己实现跳转方法 //instance的属性已经被设置好
+ (void)customRouterWithInstance:(id)instance {
if ([instance isKindOfClass:[XMExampleView class]]) {
XMExampleView *view = (XMExampleView *)instance;
NSLog(@"age=%d,name=%@",view.age,view.name);
view.frame = [UIScreen mainScreen].bounds;
[[UIApplication sharedApplication].keyWindow addSubview:view];
}
}
//对象的属性需要通过params自行设置
+ (void)customRouteWithKey:(NSString *)key andParams:(NSDictionary *)params {
XMExampleView *view = [[XMExampleView alloc] initWithFrame:[UIScreen mainScreen].bounds];
view.age = [params[@"age"] intValue];
view.name = params[@"name"];
[[UIApplication sharedApplication].keyWindow addSubview:view];
}
安装
XMRouter 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile
pod 'XMRouter'
作者
许可协议
XMRouter 在 MIT 许可协议下可用。有关更多信息,请参阅 LICENSE 文件。