通过通过url routing解决UIViewController跳转依赖

XYRouter

https://github.com/uxyheaven/XYRouter
XYRouter是一个通过url routing来解决UIViewController跳转依赖的类.
* 本类採用ARC

Installation

  • 本库基于ARC
  • 拷贝XYQuick到项目里
  • 在须要用的文件或者pch里 #import "XYRouter.h"

Podfile

pod 'XYRouter'

#import "XYRouter.h"

Usage

Creating viewController map

能够通过key和NSString来映射一个UIViewController

[[XYRouter sharedInstance] mapKey:@"aaa" toControllerClassName:@"UIViewController"];

Getting viewController for key

当取出ViewController的时候, 假设有单例[ViewController sharedInstance], 默认返回单例, 假设没有, 返回[[ViewController alloc] init].

UIViewController *vc = [[XYRouter sharedInstance] viewControllerForKey:@"aaa"];

Maping a viewController instance

假设不想每次都创建对象, 也能够直接映射一个实例

[[XYRouter sharedInstance] mapKey:@"bbb" toControllerInstance:[[UIViewController alloc] init]];

Maping a viewController instance with a block

假设想更好的定制对象, 能够用block

[[XYRouter sharedInstance] mapKey:@"nvc_TableVC" toBlock:^UIViewController *{
        TableViewController *vc = [[TableViewController alloc] init];
        UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
        return nvc;
    }];

Opening path

你能够使用key去push出一个viewController

[[XYRouter sharedInstance] openUrlString:@"aaa"];

path还支持相对路径, 如以下的代码能够在当前文件夹下push出一个TableVC后, 再push出TestVC1.

[[XYRouter sharedInstance] openUrlString:@"./TableVC/TestVC1"];

眼下支持这些描写叙述:

  • 在当前文件夹push ./
  • 在上一个文件夹push ../
  • 在根文件夹根push /

Assigning parameters

在跳转的时候还能够传递參数

@interface TestVC1 : UIViewController
@property (nonatomic, assign) NSInteger i;
@property (nonatomic, copy) NSString *str1;
@property (nonatomic, copy) NSString *str2;
@end

[[XYRouter sharedInstance] openUrlString:@"TestVC1?str1=a&str2=2&i=1"];

Changing rootViewController

能够用scheme:window替换windows.rootViewController

// rootViewController : nvc_TableVC
[[XYRouter sharedInstance] openUrlString:@"window://nvc_TableVC/TestVC1"];

Presenting rootViewController

能够用scheme:modal来呈现一个模态视图

// rootViewController : nvc_TableVC
[[XYRouter sharedInstance] openUrlString:@"modal://nvc_TableVC/TestModalVC/"];

// rootViewController : TestModalVC
[[XYRouter sharedInstance] openUrlString:@"modal://TestModalVC/?str1=a&str2=2&i=1"];

Dismissing rootViewController

关闭这个模态视图直接用dismiss

[[XYRouter sharedInstance] openUrlString:@"dismiss"];
【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/llguanli/p/8523714.html