GithubHelp home page GithubHelp logo

duoshankui / mgjrouterkit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from xtzpioneer/mgjrouterkit

0.0 1.0 0.0 73 KB

蘑菇街路由器套件,在该基础之上进行了扩展,使其更加简单快捷,主要解决了参数传递问题.利用“runtime”传递参数,同时可以传递多个参数

License: MIT License

Ruby 4.16% Objective-C 95.84%

mgjrouterkit's Introduction

MGJRouterKit

CI Status Version License Platform

说明

蘑菇街路由器套件,在该基础之上进行了扩展,使其更加简单快捷,主要解决了参数传递问题.利用“runtime”传递参数,同时可以传递多个参数

代码片段

  • MGJRouter+TXCreateObject.h文件简介
#import "MGJRouter.h"

NS_ASSUME_NONNULL_BEGIN

/** 传递参数方法键 */
FOUNDATION_EXPORT NSString *const transferParametersMethodKey;

/**
 *  蘑菇街路由器套件,在该基础之上进行了扩展,使其更加简单快捷.
 */
@interface MGJRouter (TXCreateObject)

/**
 *  创建对象
 *
 *  @param className 类名称
 *
 *  @return 创建的对象
 */
+ (id)createObjectWithClassName:(NSString *)className;

/**
 *  创建对象并传递参数
 *
 *  注意:使用该方法创建对象,必须在该对象中实现"-(void)routeWithParameters:(NSDictionary*)parameters;"方法,否则参数将传递失败
 *
 *  @param className 类名字
 *
 *  @param parameters 参数
 *
 *  @return 创建的对象
 */
+ (id)createObjectWithClassName:(NSString *)className parameters:(NSDictionary*)parameters;

@end
  • NSObject+TXTransfer.h文件简介
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/**
 *  利用“runtime”传递参数,同时可以传递多个参数
 */
@interface NSObject (TXTransfer)

/**
 *  传递参数
 *
 *  @param method 方法 (例如:"routeWithParameters:")
 *
 *  @param parameterss 参数集合 (例如:"@[parameters]")
 *
 */
- (void)transferParametersWithMethod:(NSString*)method parameterss:(NSArray*)parameterss;

@end

使用方法

  • 导入头文件MGJRouterKit.h即可使用
/** 自动注册 */
+ (void)load {
    // 模块1
    [MGJRouter registerURLPattern:@"tx://get/module1" toObjectHandler:^id(NSDictionary *routerParameters) {
        UIViewController *module1ViewController=[MGJRouter createObjectWithClassName:@"TXModule1ViewController"];
        module1ViewController.title=@"模块1";
        UINavigationController *module1NavigationViewController=[[UINavigationController alloc]initWithRootViewController:module1ViewController];
        return module1NavigationViewController;
    }];
    
    // 模块2
    [MGJRouter registerURLPattern:@"tx://get/module2" toObjectHandler:^id(NSDictionary *routerParameters) {
        UIViewController *module2ViewController=[MGJRouter createObjectWithClassName:@"TXModule2ViewController"];
        module2ViewController.title=@"模块2";
        UINavigationController *module2NavigationViewController=[[UINavigationController alloc]initWithRootViewController:module2ViewController];
        return module2NavigationViewController;
    }];
    
    // 模块3
    [MGJRouter registerURLPattern:@"tx://get/module3" toObjectHandler:^id(NSDictionary *routerParameters) {
        UIViewController *module3ViewController=[MGJRouter createObjectWithClassName:@"TXModule3ViewController"];
        module3ViewController.title=@"模块3";
        UINavigationController *module3NavigationViewController=[[UINavigationController alloc]initWithRootViewController:module3ViewController];
        return module3NavigationViewController;
    }];
    
    // 模块4
    [MGJRouter registerURLPattern:@"tx://get/module4" toObjectHandler:^id(NSDictionary *routerParameters) {
        UIViewController *module4ViewController=[MGJRouter createObjectWithClassName:@"TXModule4ViewController"];
        module4ViewController.title=@"模块4";
        UINavigationController *module4NavigationViewController=[[UINavigationController alloc]initWithRootViewController:module4ViewController];
        return module4NavigationViewController;
    }];
    
    // 测试1
    [MGJRouter registerURLPattern:@"tx://push/test1ViewController" toHandler:^(NSDictionary *routerParameters) {
        UIViewController *viewController=[MGJRouter createObjectWithClassName:@"TXTest1ViewController" parameters:routerParameters];
        [[self currentViewController].navigationController pushViewController:viewController animated:YES];
    }];
    
    // 测试2
    [MGJRouter registerURLPattern:@"tx://push/test2ViewController" toHandler:^(NSDictionary *routerParameters) {
        UIViewController *viewController=[MGJRouter createObjectWithClassName:@"TXTest2ViewController" parameters:routerParameters];
        [[self currentViewController].navigationController pushViewController:viewController animated:YES];
    }];
    
    // Web模块1
    [MGJRouter registerURLPattern:@"tx://get/webModule" toObjectHandler:^id(NSDictionary *routerParameters) {
        UIViewController *viewController=[MGJRouter createObjectWithClassName:@"TXWebViewController" parameters:routerParameters];
        return viewController;
    }];
    
    // Web模块2
    [MGJRouter registerURLPattern:@"tx://push/web" toHandler:^(NSDictionary *routerParameters) {
        UIViewController *viewController=[MGJRouter createObjectWithClassName:@"TXWebViewController" parameters:routerParameters];
        [[self currentViewController].navigationController pushViewController:viewController animated:YES];
    }];
}

/** 按钮事件 */
- (void)button2Event:(id)sender{
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"url"]=@"https://github.com/xtzPioneer/MGJRouterKit.git";
    [MGJRouter openURL:@"tx://push/web"
          withUserInfo:parameters
            completion:nil];
}

Installation

MGJRouterKit is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'MGJRouterKit'

Author

xtzPioneer, [email protected]

License

MGJRouterKit is available under the MIT license. See the LICENSE file for more info.

mgjrouterkit's People

Contributors

duoshankui avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.