路由

路由跳转的分析:

一、单例初始化路由

+ (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 *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;
    }
}

你可能感兴趣的:(路由)