- (void)push:(id)sender {
SJRouteRequest *request = [[SJRouteRequest alloc] initWithPath:@"video/playbackInfo" parameters:@{@"video_id":@(111)}];
[SJRouter.shared handleRequest:request completionHandler:^(id _Nullable result, NSError * _Nullable error) {
#ifdef DEBUG
NSLog(@"%d - %s", (int)__LINE__, __func__);
#endif
}];
}
会尝试识别路由, 找到匹配的handler,传递必要参数:
@implementation SJRouter
- (void)handleRequest:(SJRouteRequest *)request completionHandler:(SJCompletionHandler)completionHandler {
NSParameterAssert(request); if ( !request ) return;
Class<SJRouteHandler> handler = _handlersM[request.requestPath];
if ( handler ) {
[handler handleRequestWithParameters:request.requestPath topViewController:_sj_get_top_view_controller() completionHandler:completionHandler];
}
else {
printf("n (-_-) Unhandled request: %s", request.description.UTF8String);
}
}
@end
最后handler进行处理。
@implementation TestViewController
+ (NSString *)routePath {
return @"video/playbackInfo";
}
+ (void)handleRequestWithParameters:(nullable SJParameters)parameters topViewController:(UIViewController *)topViewController completionHandler:(nullable SJCompletionHandler)completionHandler {
TestViewController *vc = [TestViewController new];
vc.completionHandler = completionHandler;
[topViewController.navigationController pushViewController:vc animated:YES];
}
@end
至此, 我们再回过头看刚开始举的那个例子:
视频模块的播放页, 有与视频相关的音乐,点击这些音乐,需要跳转到音乐模块的播放页。
此时,可以让视频模块依赖Router, 进行跳转请求。这看起来都是依赖,实则两者差别很大了。
- 路由不止能处理跳转音乐模块的请求, 依赖也从多个变成只依赖Router即可。。。










