路由

作者: woniu | 来源:发表于2021-04-29 12:30 被阅读0次

路由跳转的分析:

一、单例初始化路由

+ (instancetype)route {
    static WMRoute *router = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        router = [[self alloc] init];
    });
    return router;
}

二、传参

声明相应的路由:
//代码创建 viewController
#define URL_TestAViewController @"code://TestAViewController"
//storyboard 创建 viewController
#define URL_TestBViewController @"sb://Main/TestBViewController"

    [[WMRoute route]presentWithURLString:URL_TestBViewController param:@{@"textId":@"33333",@"paraDic":paraDic} animated:YES completion:nil];

跳转路由

- (void)presentWithURLString:(NSString *)urlString
                       param:(NSDictionary * _Nullable)param
                    animated:(BOOL)animated
                  completion:(void (^ __nullable)(void))completion {
    UIViewController *viewController = [self viewControllerWithUrlString:urlString param:param];//解析路由
    UIPresentationController<UIViewControllerTransitioningDelegate> *presentationController = nil;
    presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:viewController presentingViewController:self.topViewController];
    viewController.transitioningDelegate = presentationController;
    [self.topViewController presentViewController:viewController animated:animated completion:completion];
}

三、解析路由&VC属性赋值

- (UIViewController *)viewControllerWithUrlString:(NSString *)urlString param:(NSDictionary * _Nullable)param {
    UIViewController *viewController = [urlString instantiateController];//解析路由
    viewController.hidesBottomBarWhenPushed = YES;
    if (param) {
        [param enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            @try {
                [viewController setValue:obj forKey:key];
            } @catch (NSException *exception) {}
        }];
    }
    return viewController;
}

四、instantiateController解析路由

//截取出来类名string,然后转化成Class,再初init始化回传过来,然后返回
+ (id)controllerForLink:(NSString *)link {
    if([link hasPrefix:XibScheme]) {
        return [[self class] xib_resolvingSymlinkController:[link substringFromIndex:XibScheme.length]];
    }
    else if([link hasPrefix:CodeScheme]) {
        return [[self class] code_resolvingSymlinkController:[link substringFromIndex:CodeScheme.length]];
    }
    else if([link hasPrefix:StoryboardScheme]) {
        return [[self class] storyboard_resolvingSymlinkController:[link substringFromIndex:StoryboardScheme.length]];
    } else {
        NSLog(@"not support scheme %@", link);
        return nil;
    }
}

- (id)instantiateController {
    return [[self class] controllerForLink:self];
}

五、反射出来类名,并初始化

NSString * const StoryboardScheme = @"sb://";
NSString * const XibScheme        = @"xib://";
NSString * const CodeScheme       = @"code://";

+ (id)storyboard_resolvingSymlinkController:(NSString*)controllerLink {
    NSArray *parts = [controllerLink componentsSeparatedByString:@"/"];

    NSString *storyboardName = [parts objectAtIndex:0];
    NSString *controllerId = parts.count == 2 ? [parts objectAtIndex:1] : nil;

    UIViewController *controller = nil;
    @try {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];

        if (controllerId) {
            controller = [storyboard instantiateViewControllerWithIdentifier:controllerId];
        } else {
            controller = [storyboard instantiateInitialViewController];

            if (!controller) {
                NSLog(@"may be you forgot to set initial view controller in %@.storyboard", storyboardName);
            }
        }
    }
    @catch (NSException *exception) {
        NSLog(@"Error:can not load controller <%@>, reason:%@", controllerLink, exception);
    }
    @finally {
        return controller;
    }
}

+ (id)xib_resolvingSymlinkController:(NSString*)controllerLink {
    UIViewController *controller = nil;
    
    @try {
        NSString *nibName         = controllerLink;
        NSString *className       = controllerLink;
        Class     controllerClass = NSClassFromString(className);
        controller = [[controllerClass alloc] initWithNibName:nibName bundle:nil];
    }
    @catch (NSException *exception) {
        NSLog(@"Error:can not load controller <%@>, reason:%@", controllerLink, exception);
    }
    @finally {
        return controller;
    }
}

+ (id)code_resolvingSymlinkController:(NSString*)controllerLink {
    UIViewController *controller = nil;
    
    @try {
        NSString *className = controllerLink;
        Class controllerClass = NSClassFromString(className);
        controller = [[controllerClass alloc] init];
    }
    @catch (NSException *exception) {
        NSLog(@"Error:can not load controller <%@>, reason:%@", controllerLink, exception);
    }
    @finally {
        return controller;
    }
}

相关文章

  • thinkphp5学习笔记(三)路由配置

    URL请求的执行流程 路由模式 路由注册 路由规则 路由地址 路由参数 变量规则 路由分组 别名路由 路由绑定

  • larevel 路由索引

    基本路由:路由重定向、视图路由路由参数:必选、可选、正则表达式命名路由路由组:中间件、命名空间、子域名路由、路由前...

  • laravel路由

    2 路由格式 3 路由参数 4 .路由别名 5 .路由群组

  • Vue3: 前端路由的概念和原理

    1、什么是路由 路由(英文:router)就是对应关系。路由分为两大类:① 后端路由② 前端路由 2、后端路由 后...

  • 组件化2.路由框架的设计

    路由框架原理 路由框架是为了实现组件之间的通信 路由框架维护了一个分组的路由表路由表中存放了路由地址和路由信息路由...

  • vue中的路由

    vue中4中路由包含: 1、动态路由2、嵌套路由3、编程式路由4、命名式路由 1、动态路由 2、嵌套路由 3、编程...

  • React-Router知识点

    路由的分类 页面路由 hash 路由 h5路由 react路由 react-router-dom 路由方式 h5路...

  • 路由策略

    路由策略和策略路由 什么是路由策略?路由策略和策略路由有什么区别? 如何配置路由策略? https://blog....

  • Laravel 学习笔记

    路由 文件位置: app/Http/routes.php 基础路由get/post 多请求路由 路由参数 路由别名...

  • React路由

    React路由 一、路由的形式 hash路由 : HashRouter history路由 : BrowserRo...

网友评论

      本文标题:路由

      本文链接:https://www.haomeiwen.com/subject/nctwrltx.html