应用路由图
NTURLMap 解决了反复编写 viewContrller 创建代码的问题。它要求你在应用程序中具有 oNT URLMap,以提供表示从哪里来和去唯一 URL。在处理参数传递和未定义的块时,由于涉及许多运行时技术,所以使用时可能会感到困惑。
最简单创建方法
[[NTURLMap globalMap] addSchemeWithURL:@"test://home" toViewController:[UIViewController class]];
[[NTURLMap globalMap] addSchemeWithURL:@"test://home" toViewController:[UIViewController class] initializeSelector:@selector(init)];
//Usage
[self.navigationController.navigator navigateToURL:@"test://home" animated:YES arguments:nil];
请注意,我们在这里不需要传递参数,因为 initializeSelector 不需要参数,并且在添加方案方法中也没有传递 initializeBlock。
使用块创建,当视图控制器创建后,会调用 InitizlizedBlock,您可以在块中进行一些后续工作。
[[NTURLMap globalMap] addSchemeWithURL:@"test://home" toViewController:[UIViewController class] initializeSelector:@selector(init) initializeBlock:(NTNavigator *navigator,UIViewController *viewController,NSString *title){
viewController.title = title;
}];
//Usage
[self.navigationController.navigator navigateToURL:@"test://home" animated:YES arguments:@"title"];
我们为什么在这里不需要将 navigator 和 viewController 作为参数传递?好问题!因为我们不必这么做!initializeBlock 总是在用户传递的参数之前,传递 navigator 实例和 viewController!
创建需要选择器和块都需要参数的方案
[[NTURLMap globalMap] addSchemeWithURL:@"test://home" toViewController:[UIViewController class] initializeSelector:@selector(initWithNibName:bundle:) initializeBlock:(NTNavigator *navigator,UIViewController *viewController,NSString *title){
viewController.title = title;
}];
//Usage
[self.navigationController.navigator navigateToURL:@"test://home" animated:YES arguments:@"UIViewController",nil,@"title"];
由于 initWithNibName:bundle: 需要两个参数,块需要一个参数,因此我们需要在导航到 URL @"test://home" 时传递这些参数。
使用 Nib 创建
[[NTURLMap globalMap] addSchemeWithURL:@"test://home" toViewController:[UIViewController class] nibName:@"ViewController" inBundle:[NSBundle mainBundle]];
很简单 :)