iOS轻量级组件化管理工具,让你轻轻松松添加自己的小组件.

TXRouter是一款轻量级的iOS组件化管理工具,它提供了简单易用的API来帮助开发者轻松实现模块间的跳转及参数传递。本文介绍了TXRouter的基本使用方法,并通过示例展示了如何创建和调用不同类型的视图控制器。
摘要由CSDN通过智能技术生成

TXRouter

  • 轻量级组件化管理工具,让你轻轻松松添加自己的小组件.该工具原理简单、制作轻松、思路清晰等优点.

TXRouter优点

  • 比MGJRouter更加简单、使用更加方便
  • 原理简单、制作轻松、思路清晰

TXModel缺点

  • 不能高大上定义URL

代码片段

/*路由管理器*/
+ (TXRouter*)routerManager;

/**
 * 创建对象
 * @param className 类名字
 */
- (id)createObjectWithClassName:(NSString *)className;

/**
 * 创建对象
 * @param className 类名字
 * @param parameters 传递的参数
 */
- (id)createObjectWithClassName:(NSString *)className parameters:(NSDictionary*)parameters;

使用方法

  • #import "TXRouter.h"
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"TX路由工具";
    
    CGFloat viewW=self.view.frame.size.width;
   
    CGFloat spacing=20;
    CGFloat buttonH=30;
    CGFloat buttonX=spacing;
    CGFloat buttonW=viewW-buttonX*2;
    
    CGFloat button1Y=100;
    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, button1Y, buttonW, buttonH)];
    [button1 setTitle:@"访问VC1" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button1.tag = 1;
    [button1 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    CGFloat button2Y=CGRectGetMaxY(button1.frame)+spacing;
    UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, button2Y, buttonW, buttonH)];
    [button2 setTitle:@"访问VC2" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button2.tag = 2;
    [button2 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    CGFloat button3Y=CGRectGetMaxY(button2.frame)+spacing;
    UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, button3Y, buttonW, buttonH)];
    [button3 setTitle:@"访问VC3(代码块传值)" forState:UIControlStateNormal];
    [button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button3.tag = 5;
    [button3 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
    
    CGFloat butto3Y=CGRectGetMaxY(button3.frame)+spacing;
    UIButton *butto3 = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, butto3Y, buttonW, buttonH)];
    [butto3 setTitle:@"访问Web页面" forState:UIControlStateNormal];
    [butto3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    butto3.tag = 3;
    [butto3 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:butto3];
    
    CGFloat button4Y=CGRectGetMaxY(butto3.frame)+spacing;
    UIButton *button4 = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, button4Y, buttonW, buttonH)];
    [button4 setTitle:@"访问未定义的页面" forState:UIControlStateNormal];
    [button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button4.tag = 4;
    [button4 addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button4];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)back:(UIButton *)btn{
    switch (btn.tag) {
        case 1:{
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
            [dic setValue:@"hello world" forKey:@"title"];
            UIViewController *viewController = [[TXRouter routerManager] createObjectWithClassName:@"TXTest1ViewController" parameters:dic];
            if (viewController) {
                [self.navigationController pushViewController:viewController animated:YES];
            }
        }
            break;
        case 2:{
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
            [dic setValue:@"hello world" forKey:@"title"];
            UIViewController *viewController = [[TXRouter routerManager] createObjectWithClassName:@"TXTest2ViewController" parameters:dic];
            if (viewController) {
                [self presentViewController:viewController animated:YES completion:nil];
            }
        }
            break;
        case 3:{
            NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
            [dic setValue:@"https://www.baidu.com" forKey:@"url"];
            [dic setValue:@"百度一下" forKey:@"title"];
            UIViewController *viewController = [[TXRouter routerManager] createObjectWithClassName:@"TXTestWebViewController" parameters:dic];
            if (viewController) {
                [self.navigationController pushViewController:viewController animated:YES];
            }
            
        }
            break;
        case 5:{
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
            //声明代码块
            void (^textBlock) (NSString *msg);
            //可以将block加入字典,当做一个回调取值
            textBlock = ^(NSString *msg){
                [btn setTitle:[NSString stringWithFormat:@"访问VC3(%@)",msg] forState:UIControlStateNormal];
                NSLog(@"从VC3中获取的数据是===>%@",msg);
            };
            [dic setObject:textBlock forKey:@"block"];
            
            UIViewController *viewController = [[TXRouter routerManager] createObjectWithClassName:@"TXTest3ViewController" parameters:dic];
            if (viewController) {
                [self presentViewController:viewController animated:YES completion:nil];
            }
            
        }
            break;
        case 4:{
            UIViewController * viewController = [[TXRouter routerManager] createObjectWithClassName:@"TXUnknowvc" parameters:nil];
            if (viewController) {
                [self presentViewController:viewController animated:YES completion:nil];
            }
            
        }
        default:
            break;
    }
}

查看项目源码请点这里!

项目结构展示

image

效果展示

转载于:https://my.oschina.net/u/2483781/blog/1821833

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值