IOS uses Router mechanism for dynamic jump ViewController

Overview:

In a multi person development environment, an ViewController often introduces a large number of other ViewController, such as: FirstViewController introduces SecondViewController and ThirdViewController.

We must meet the following scenario:

1., at times, changes in requirements often modify the ViewController name, which in turn results in individual files that need to be manually modified. 2., if two people in the development of two ViewController, if the classmate A need to introduce students B Controller, can only let students B developers first this class submit, or have other solutions. 3., etc.

In short, in iOS Develop, the word “XxxViewController.h #import” is the highest frequency. Especially now, Xcode is often unable to find other class names and is dying!

Here I will introduce you to a new ViewController jump mode, from now on no longer need import other classes.

Open Source Framework HHRouter

HHRouter: https://github.com/Huohua/HHRouter

HHRouter does not depend on other libraries, you implement a simple Mapping algorithm, performance is particularly good, better than similar Router framework, it is recommended to use, because of the single function, so the performance is very strong, Simple and Reliable!

Simple Demo

We just need to introduce the following code into the AppDelegate.m:

[[HHRouter, shared], map:@, one, toControllerClass:[OneViewController, class]];

Now, when we jump the OneViewController in any ViewController, we don’t need the file in import, just need to:

ViewController *vc = [[HHRouter, shared], matchController:@, “one”]; [self.navigationController, pushViewController:vc, animated:YES];

Does it feel so simple? So easy!

If you just want the page to jump, you don’t have to think about passing the value so far.

Advanced!

Value

If you want to jump to the property and pass the value, you just need to change the following definition when you define Router:

[[HHRouter shared] map:@ “/user/: userId/” toControllerClass:[UserViewController class]];

When jumping, change as follows:

ViewController *vc = [[HHRouter, shared], matchController:@, “/user/1/”]; [self.navigationController, pushViewController:vc, animated:YES];

Join the test in UserViewController:

NSLog (@ “% @ self.params[@”, “userId”]);

Output: 1

principle

Because when you use URL to pass values, HHRouter puts the corresponding key and value in the params dictionary, and the params attribute is defined in the UIViewController Category:

@interface, UIViewController (HHRouter), @property (nonatomic, strong), NSDictionary, *params; @end

URL query parameters

The URL query argument is also supported and is very simple to use!

UIViewController *viewController = [[HHRouter shared] matchController:@ “/user/1/? TabIndex=3”]; NSLog (@ “% @ viewController.params[@”, “tabIndex”]);

Output: 3

URL Schemes

If your app has defined some URL schemes, HHRouter can also be solved.

UIViewController, *viewController = [[HHRouter, shared], matchController:@, “hhrouter://user/1/”];

[viewController class] equals [UserViewController class]

The value of params[@ “userId” in UserViewController is equal to 1

Block

– – (void) map: (NSString *) route toBlock: (HHRouterBlock) block; – – (HHRouterBlock) matchBlock: (NSString *) route; – – (ID) callBlock: (NSString *) route;

HHRouter also supports Block callbacks, and if interested students can view the.H file, both the usage and the view jumps are the same!

Block definition

[[HHRouter shared] map:@ “block1” toBlock:^id (NSDictionary *params) {NSLog (“% @ @ @ params”, “test”); return;}];

Since the return value is of type ID, you can return the array, dictionary, and so on!

There are two ways to call:

First kind:

HHRouterBlock block = [[HHRouter shared] matchBlock:@ “block1”];
NSLog (@ “% @ block” (“aaaaaa” @{@: @ “AAAAA”}));

Output:

{aaaaaa = AAAAA; block = “< __NSGlobalBlock__: 0x1081a5120>”; route = block1;} test

Second kinds:

NSLog (@ “% @”, [[HHRouter shared] callBlock:@ “block1”]);

Output:

{block = “< __NSGlobalBlock__: 0x1081a5120>”; route = block1;} test

Did the attentive student see it?

The first call returns a block that can be passed inside.

The second is direct invocation without the value passing operation.

You can choose according to your business needs.

install

CocoaPods

Pod,’HHRouter’,’~> 0.1.5′

Manual

Direct the GitHub and HHRouter.m files inside HHRouter.h into your project.

In the ViewController you need, import:

#import “HHRouter.h””

Licenceļ¼š

HHRouter follows the MIT license