通过通过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"];

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-29 07:26:26

通过通过url routing解决UIViewController跳转依赖的相关文章

转:【WebDriver】封装GET方法来解决页面跳转不稳定的问题

在大多数测试环境中,网络或者测试服务器主机之间并不是永远不出问题的,很多时候一个客户端的一个跳转的请求会因为不稳定的网络或者偶发的其它异常hang死在那里半天不动,直到人工干预动作的出现.      而WebDriver测试执行时,偶然也会因此发生页面跳转或者加载的超时异常,而使得流程性的测试中断,给测试完整性和有效性带来很大的损失.其实这种问题很好解决,只需要重写或者封装一下GET方法来实现跳转就行了.      在做这个封装之前,我们得事先讲一下driver.get(url)和driver.

URL Routing

们知道在ASP.NET Web Forms中,一个URL请求往往对应一个aspx页面,一个aspx页面就是一个物理文件,它包含对请求的处理. 而在ASP.NET MVC中,一个URL请求是由对应的一个Controller中的Action来处理的,由URL Routing来告诉MVC如何定位到正确的Controller和Action. 笼统的讲,URL Routing包含两个主要功能:解析URL 和 生成URL,本文将围绕这两个大点进行讲解. 本文目录 URL Routing 的定义方式 让我们从

php防止伪造数据从URL提交解决方法

php防止伪造的数据从URL提交方法. 针对伪造的数据从URL提交的情况,首先是一个检查前一页来源的如下代码: <?/*PHP防止站外提交数据的方法*/ function CheckURL(){ $servername=$_SERVER['SERVER_NAME']; $sub_from=$_SERVER["HTTP_REFERER"]; $sub_len=strlen($servername); $checkfrom=substr($sub_from,7,$sub_len);

【ASP.NET】同一URL根据不同设备跳到不同页面

我们都知道,现在的新大型网站,如果输入www开头的网址,网站会自动判别设备,并跳转到合适的页面,来自于PC和移动端最终访问的真实url会不同.解决方案如下: 在网站的默认页面Defau.aspx的Default.aspx.cs文件的Page_Load方法中加入如下代码: string u = Request.ServerVariables["HTTP_USER_AGENT"]; Regex b = new Regex(@"android.+mobile|avantgo|bad

ASP.NET MVC 学习之路由(URL Routing)

在ASP.NET MVC中,一个URL请求是由对应的一个Controller中的Action来处理的,由URL Routing来告诉MVC如何定位到正确的Controller和Action. 默认路由表 当我们在VS中创建一个新的 ASP.NET MVC程序,程序将会自动使用默认的路由表. public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInf

使用&lt;jsp:include&gt;,不想写死URL,动态生成URL的解决办法

JSP中文件包含有2种方式,静态包含和动态包含.静态包含使用<%@ include file="" %>,动态包含使用<jsp:include page="" />.本文不打算介绍这2种方式的区别和使用场景,主要关注page和file属性的路径问题. 如果事先知道被包含页面的url,那么直接写死到page或file属性中即可.这种写死url的方式,没有什么不好,但是非常不灵活,不能满足动态生成url的需求.考虑这种场景,A.jsp访问B.js

SQLNestedException: Cannot create JDBC driver of class &#39;&#39; for connect URL &#39;null&#39; 解决办法

当跑jndi项目时抛出:org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'异常 解决办法: 1.在Tomcat 6.0\lib下加入oracle驱动包ojdbc14_g-10.2.0.1.0.jar 2.在Tomcat 6.0\conf下的context.xml文件中加入 [javascript] view plaincopy <R

[译]Angular-ui 之 Url Routing

? 前一篇 (Multiple Named Views)     下一篇 (The Components) ? 在你的应用中多数的状态都是基于特定的url地址的.Url Routing机制绝不是在状态机制之上后加的东西,而是一开始就是规划在最初设计方案(译注:angular-ui的设计方案)之中的(在实现url路由的同时独立的保持状态).下面代码展示了你如何设置一个url: Most states in your application will probably have a url asso

控制器 - URL routing HTTP module(一)

URL routing HTTP module 负责处理检查入站请求的 URL,并将它们分派到最合理的处理器上.URL routing HTTP module 也替代了旧版本的 ASP.NET URL 重写特性.核心方面,URL 重写由 连接请求,转换原始 URL,指导 HTTP 运行时环境处理一个“最可能相关但存在区别”的 URL 这几个部分组成. 替代 URL 重写 如果我们需要在路由可读性.面向搜索引擎友好和需要以编程的方式处理若干的 URLs 方面上做权衡,那么,URL 重写就发挥作用了