iOS使用核心的50行代码撸一个路由组件

2020-01-21 07:11:07于丽

1、不需要返回值

如下注册"home/messagelist"的是一个页面跳转的路由,actionBlock的参数是一个YTRouterActionCallbackObject对象,可以从YTRouterActionCallbackObject对象或者到参数,关于如何传递值,会在下面的客户端调用者使用这里讲到。然后在actionBlock处理目的页面的初始化、参数设置等步骤,然后执行页面跳转。

2、需要返回值

如下注册"home/messagelist/getmessage"的是一个提供返回值的路由,同样也可以从YTRouterActionCallbackObject对象获取参数,另外YTRouterActionCallbackObject对象还有一个actionCallbackBlock属性是专门处理返回参数给调用者的,如下的代码只是简单返回一个字符串,在更加具体的业务场景中,这里会设置接口调用、数据库查询等任务,最后把结果返回。


@implementation ModuleAUriRegister
+ (void)load {
 [[YTRouterManager sharedRouterManager] registerPath:@"home/messagelist" actionBlock:^(YTRouterActionCallbackObject *callbackObject) {
  MessageListViewController *messageListVC = [MessageListViewController new];
  NSString *title = callbackObject.uri.params[@"title"];
  messageListVC.title = title;
  [[UIViewController yt_currentViewControlloer].navigationController pushViewController:messageListVC animated:YES];;
 }];
 [[YTRouterManager sharedRouterManager] registerPath:@"home/" actionBlock:^(YTRouterActionCallbackObject *callbackObject) {
 }];
 [[YTRouterManager sharedRouterManager] registerPath:@"home/messagelist/detail" actionBlock:^(YTRouterActionCallbackObject *callbackObject) {
 }];
 [[YTRouterManager sharedRouterManager] registerPath:@"home/messagelist/getmessage" actionBlock:^(YTRouterActionCallbackObject *callbackObject) {
  // 内容回调
  !callbackObject.actionCallbackBlock ?: callbackObject.actionCallbackBlock(@"message content text demo");
 }];
}
@end

客户端调用者使用

1、简单的path跳转调用

使用YTRouterManager单例对象的runWithPath方法,传递一个注册的path参数完成跳转。