- Router路由方案
- Target-Action
- Protocol
-
页面间的跳转采用路由
// 配置路由 - (void)configRouter { [Router registerName:@"Test" ClassName:@"TestViewController"]; [Router registerName:@"Comment" ClassName:@"CommentViewController"]; }
-
打开一个页面
[Router openName:@"Test" params:@{}];
-
打开一个页面有回调
[Router openName:@"Test" params:@{} callBack:^(NSString *name, id params) { NSLog(@"页面名称:%@ 回调内容:%@",name,params); }];
-
导入对应的协议以及协议管理
#import "BWService.h" #import "CommentProtocol.h" #import "LoginProtocol.h"
-
调用协议的方法
id<LoginProtocol> login = [[BWService sharedManger]serviceInstanceWithProtocol:@protocol(LoginProtocol)]; [login openLogin]; id<CommentProtocol> comment = [[BWService sharedManger]serviceInstanceWithProtocol:@protocol(CommentProtocol)]; [comment openComment]; [comment getComment:^(NSString *text) { NSLog(@"获取评论啊==========%@",text); }];
- 规范了页面间的跳转,A,B页面无需互相引用头文件。
- 模块间的通讯采用协议,模块具备封装,以及单一性的特点。
- 调用者之需要知道模块的协议提供了什么方法,无需了解内部如何实现,具备“最少了解的原则”。
-
路由的解析可以参考蘑菇街,这里不再嗷述。
-
路由的调整是否只能push或者present呢?可以定义一个协议,若视图控制器实现了这个协议,则可以自定义跳转。
-
定义路由协议
typedef void (^RouterCallBack)(NSString *name,id params); @protocol RouterProtocol <NSObject> @optional - (void)openVc:(UIViewController *)vc; - (void)eventCallBack:(RouterCallBack)callBack; @end
-
判断控制器是否实现open方法
// 实现了协议 if ([target conformsToProtocol:@protocol(RouterProtocol)]) { id <RouterProtocol> helloVc = target; if ([helloVc respondsToSelector:@selector(openVc:)]) { [helloVc openVc:[self sharedInstance].rootVc]; } else { [[self sharedInstance]openTarget:target]; } if ([helloVc respondsToSelector:@selector(eventCallBack:)]) { [helloVc eventCallBack:callback]; } return target; } // 未实现 else { [[self sharedInstance]openTarget:target]; }
-
视图控制器实现了协议,则跳转相关交给控制器自由定义。
@interface CommentViewController ()<RouterProtocol> @end @implementation CommentViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"评论模块"; // Do any additional setup after loading the view from its nib. } - (void)openVc:(UIViewController *)vc { [vc presentViewController:self animated:YES completion:nil]; }
-
对于回调的处理也是同理
if ([helloVc respondsToSelector:@selector(eventCallBack:)]) { [helloVc eventCallBack:callback]; } ... // vc @property(nonatomic, copy) RouterCallBack callback; ... - (void)sendMessage { if (self.callback) { self.callback(@"Test", @{@"name":@"hello"}); } } - (void)eventCallBack:(RouterCallBack)callBack { self.callback = callBack; }
-
需要拓展更多功能的方法,只需要在RouterProtocol添加。