[iOS] 组件化之一:MGJRouter的简单实用

在大型APP开发过程会用到iOS的组件化的相关技术,这里不得不提到路由控制,虽然路由并不是组件化的核心,但是它是构造大型复杂系统的基础。

目前国内的蘑菇街团队提供了MGJRouter路由库,使用比较简单,其原理主要是通过注册URL来实现路由跳转。主要有两个步骤:

1、注册URL生成路由表,2.openUrl实现跳转。

下面是一个简单的使用demo:

一、我们创建一个类RouterManager,然后在+(void)load方法里面统一注册url

#import 

@interface RouterManager : NSObject

@end
#import "RouterManager.h"
#import "MGJRouter.h"
#import "TestViewController.h"
#import "Test2ViewController.h"
#import "Test3ViewController.h"
@implementation RouterManager

+ (void)load {
    [MGJRouter registerURLPattern:@"MGJ://Test1/PushMainVC" toHandler:^(NSDictionary *routerParameters) {
        UINavigationController *navigationController = routerParameters[MGJRouterParameterUserInfo][@"navigationVC"];
        TestViewController *testVC = [[TestViewController alloc] init];
        [navigationController pushViewController:testVC animated:YES];
    }];
    
    [MGJRouter registerURLPattern:@"MGJ://Test2/PushMainVC" toHandler:^(NSDictionary *routerParameters) {
        UINavigationController *navigationControler = routerParameters[MGJRouterParameterUserInfo][@"navigationVC"];
        NSString *labelText = routerParameters[MGJRouterParameterUserInfo][@"text"];
        Test2ViewController *test2 = [[Test2ViewController alloc] init];
        test2.labelText = labelText;
        [navigationControler pushViewController:test2 animated:YES];
        
    }];
    
    [MGJRouter registerURLPattern:@"MGJ://Test3/PushMainVC" toHandler:^(NSDictionary *routerParameters) {
        UINavigationController *navigationControler = routerParameters[MGJRouterParameterUserInfo][@"navigationVC"];
        
        void(^block)(NSString *) = routerParameters[MGJRouterParameterUserInfo][@"block"];
        Test3ViewController *test3 = [[Test3ViewController alloc] init];
        test3.btnClickBlock = block;
        [navigationControler pushViewController:test3 animated:YES];
    }];
    
    [MGJRouter registerURLPattern:@"MGJ://Test2/getMainVC" toObjectHandler:^id(NSDictionary *routerParameters) {
        NSString *labelText = routerParameters[MGJRouterParameterUserInfo][@"text"];
        Test2ViewController *vc = [[Test2ViewController alloc] init];
        vc.labelText = labelText;
        return vc;
    }];
}

@end

二、调用:

#import "ViewController.h"
#import "MGJRouter.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)pushVC:(id)sender {
    
    [MGJRouter openURL:@"LXY://Test1/PushMainVC" withUserInfo:@{@"navigationVC":self.navigationController} completion:nil];
}

- (IBAction)passNextClick:(id)sender {
    [MGJRouter openURL:@"MGJ://Test2/PushMainVC" withUserInfo:@{@"navigationVC":self.navigationController,@"text":@"前向传值"} completion:nil];
}
- (IBAction)blockBtnClick:(id)sender {
    [MGJRouter openURL:@"MGJ://Test3/PushMainVC" withUserInfo:@{@"navigationVC":self.navigationController,
                       @"block":^(NSString *text){
                            NSLog(@"%@",text);
                      }} completion:nil];
}
- (IBAction)backClick:(id)sender {
    [self.navigationController pushViewController:[MGJRouter objectForURL:@"MGJ://Test2/getMainVC" withUserInfo:@{@"text":@"dsfdsf"}] animated:YES];
}

注意:第二个按钮跳转后同时给下个VC传值,第三个对应跳转后回调传值。

你可能感兴趣的:(iOS)