AMKDispatcher - 模块化接口,跨模块操作分发
示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
。
要求
安装
AMKDispatcher 通过 CocoaPods 提供。要安装它,只需要在您的 Podfile 中添加以下行
pod 'AMKDispatcher'
用法
1. 按照示例工程中的用法,创建相关类
- AMKTarget_Demo.h
#import <Foundation/Foundation.h>
@interface AMKTarget_Demo : NSObject
/// 测试:前往某页面
- (void)Action_gotoViewControllerWithParams:(NSDictionary *)params;
/// 测试:未实现分发的转发
- (void)Action_forwardTargetActionWithParams:(NSDictionary *)params;
/// 测试:分发并返回对象类型的值
- (id)Action_alertDispatcherResult:(NSDictionary *)params;
/// 测试:分发并返回基础数据类型的值
- (NSInteger)Action_alertDispatcherResult2:(NSDictionary *)params;
@end
- AMKTarget_Demo.m
#import "AMKTarget_Demo.h"
#import "AMKViewController.h"
@implementation AMKTarget_Demo
- (void)Action_gotoViewControllerWithParams:(NSDictionary *)params {
UIWindow *window = [UIApplication sharedApplication].delegate.window;
UINavigationController *navigationController = (UINavigationController *)window.rootViewController;
if ([navigationController isKindOfClass:[UINavigationController class]]) {
[navigationController pushViewController:AMKViewController.new animated:YES];
}
}
- (void)Action_forwardTargetActionWithParams:(NSDictionary *)params {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"未识别Action" message:params.description preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}]];
[[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:alertController animated:YES completion:nil];;
}
- (void)Action_alertWithoutParams {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"AlertWithoutParams" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}]];
[[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:alertController animated:YES completion:nil];;
}
- (id)Action_alertDispatcherResult:(NSDictionary *)params {
return [params objectForKey:@"text"];
}
- (NSInteger)Action_alertDispatcherResult2:(NSDictionary *)params {
return 20180105;
}
@end
2. 注册(如下是AMKDispatcher的相关属性和接口)
/** Target-Ation 分发服务 */
@interface AMKDispatcher : NSObject
/** 单例 */
@property(nonatomic, strong, readonly, nonnull, class) AMKDispatcher *sharedInstance;
/** TargetName前缀,默认值为@"AMKTarget_";请务必使用执行 -performTarget:action:params:shouldCacheTarget: 前赋值 */
@property(nonatomic, copy, nonnull) NSString *targetPrefix;
/** ActionName前缀,默认值为@"Action_";请务必使用执行 -performTarget:action:params:shouldCacheTarget: 前赋值 */
@property(nonatomic, copy, nonnull) NSString *actionPrefix;
/** 未识别指定TargetName时转发的TargetName,默认值为@"Forwarding";请务必使用执行 -performTarget:action:params:shouldCacheTarget: 前赋值 */
@property(nonatomic, copy, nonnull) NSString *forwardTargetName;
/** 未识别指定TargetName时转发的TargetName,默认值为@"forwardTargetActionWithParams:";请务必使用执行 -performTarget:action:params:shouldCacheTarget: 前赋值 */
@property(nonatomic, copy, nonnull) NSString *forwardActionName;
/** 模块内组件调用入口 */
- (id _Nullable)performTarget:(NSString * _Nullable)targetName action:(NSString * _Nullable)actionName params:(NSDictionary * _Nullable)params shouldCacheTarget:(BOOL)shouldCacheTarget;
+ (id _Nullable)performTarget:(NSString * _Nullable)targetName action:(NSString * _Nullable)actionName params:(NSDictionary * _Nullable)params shouldCacheTarget:(BOOL)shouldCacheTarget;
/** 移除指定名称的CachedTarget */
- (void)removeCachedTargetWithTargetName:(NSString * _Nullable)targetName;
+ (void)removeCachedTargetWithTargetName:(NSString * _Nullable)targetName;
@end
3. 分发
id object = [[AMKDispatcher sharedInstance] performTarget:@"Demo" action:@"gotoViewControllerWithParams:" params:@{} shouldCacheTarget:YES];
作者
AndyM129, [email protected]
许可
AMKDispatcher遵循MIT许可协议。更多信息请查看LICENSE文件。