#import "FirstViewController.h"
FirstViewController *controller = [[FirstViewController alloc] init];
[self.navigationController pushViewController:controller animated:YES];
上面这段代码是ios开发中很常见的一段代码,但是这平常无奇的代码却有一个隐患,这个隐患在随项目不断扩展会越来越严重。那就是
ViewController之间是存在耦合的,想要跳转目标ViewController,则必须引入对应的类头文件。更有甚者,在ViewController的.h
文件中暴露属性和方法,简直无法直视。这次主要解决的问题就是彻底剪断ViewController之间的耦合,清理ViewController的.h
文件中暴露的内容,还一个清爽的ViewController。
流程图
自定义全局导航栏
- 初始化导航栏
因为需要统一对目标ViewController初始化,增删改查等操作,需要自定义一个全局导航栏,为了方便处理,把导航栏做成单例。
// WBNavigationController.h
@interface WBNavigationController : UINavigationController
+ (instancetype)sharedInstance;
@end
// WBNavigationController.m
@interface WBNavigationController ()
// 保存所有注册的ViewController的URL与类名
@property (nonatomic, strong) NSMutableDictionary *registerVCCls;
@end
@implementation WBNavigationController
+ (instancetype)sharedInstance {
static WBNavigationController *navigationController = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
navigationController = [[WBNavigationController alloc] init];
});
return navigationController;
}
- 导航栏操作
// WBNavigationController.m
// 注册一个ViewController到导航栏
+ (void)registerWithUrl:(NSString *)url viewControllerClass:(Class)cls {
[WBNavigationController sharedInstance].registerVCCls[url] = cls;
}
// 移除导航栏中一个ViewController的实例
+ (void)removeViewControllerWithUrl:(NSString *)url {
NSMutableArray *viewControllers = [[WBNavigationController sharedInstance].viewControllers mutableCopy];
UIViewController *targetVC = [[self class] findViewControllerIfExistWithUrl:url];
if ( [viewControllers containsObject:targetVC] ) {
[viewControllers removeObject:targetVC];
}
[WBNavigationController sharedInstance].viewControllers = viewControllers;
}
// 根据url查找ViewController的类名
+ (Class)findViewControllerClassWithUrl:(NSString *)url {
return [WBNavigationController sharedInstance].registerVCCls[url];
}
// 导航栏中是否存在ViewController的实例
+ (BOOL)existViewControllerWithUrl:(NSString *)url {
NSMutableArray *viewControllers = [[WBNavigationController sharedInstance].viewControllers mutableCopy];
UIViewController *targetVC = [[self class] findViewControllerIfExistWithUrl:url];
if ( [viewControllers containsObject:targetVC] ) {
return YES;
}
return NO;
}
// 获取导航栏中的ViewController的实例
+ (UIViewController *)findViewControllerIfExistWithUrl:(NSString *)url {
Class vcClassName = [[self class] findViewControllerClassWithUrl:url];
for (UIViewController *vc in [WBNavigationController sharedInstance].viewControllers) {
if ( vcClassName == vc.class ) {
return vc;
}
}
return nil;
}
// 取消注册
+ (void)deregisterUrl:(NSString *)url {
[[WBNavigationController sharedInstance].registerVCCls removeObjectForKey:url];
}
ViewController类别
为了方便调用,给ViewController添加一个类别用于调用导航栏的操作。
- 初始化目标ViewController
在.h
头文件中暴露属性与方法无非就是传递参数,与适时的回调。为了清除这些,给每个目标ViewController添加参数传递与回调block。
#import "UIViewController+URL.h"
@interface UIViewController (URL)
// 给目标ViewController传递的参数
@property (nonatomic, strong) id wb_params;
// 给目标ViewController的回调
@property (nonatomic, copy) WBReplyAction wb_replyAction;
// 初始化目标ViewController
- (instancetype)initWithParams:(id)params;
- (instancetype)initWithParams:(id)params replyAction:(WBReplyAction)replyAction;
- 封装导航栏操作
封装导航栏常用操作,push,pop,以及目标ViewController的present&&dismiss操作。以下以push为例。
#import "UIViewController+URL.h"
// push操作
- (void)wb_pushViewController:(WBParams)params;
- (void)wb_pushSimpleViewController:(NSString *)url;
- (void)wb_popViewController;
- (void)wb_popViewControllerAnimate:(BOOL)animated;
- (void)wb_popToRootViewControllerAnimated:(BOOL)animated;
- (void)wb_popToViewControllerWithUrl:(NSString *)url animated:(BOOL)animated;
- (void)wb_presentViewController:(WBParams)params;
- (void)wb_presentSimpleViewController:(NSString *)url;
- (void)wb_dismissSimpleViewController;
- (void)wb_dismissViewControllerAnimated:(BOOL)animated completion:(WBCompleteAction)completion;
#import "UIViewController+URL.m"
- (void)wb_pushSimpleViewController:(NSString *)url {
[self wb_pushViewController:^(WBNode *node) {
node.url = url;
}];
}
- (void)wb_pushViewController:(WBParams)params {
WBNode *node = [self setupNode:params];
Class vcClass = [WBNavigationController findViewControllerClassWithUrl:node.url];
if ( !vcClass ) {
NSLog(@"URL:%@ not register", node.url);
}
UIViewController *controller = [[vcClass alloc] initWithParams:node.params replyAction:node.replyAction];
[[WBNavigationController sharedInstance] pushViewController:controller animated:node.animate];
}
- 测试调用
- 在AppDelegate设置window的rootViewController为全局导航栏。
FirstViewController *rootViewController = [[FirstViewController alloc] init];
[[WBNavigationController sharedInstance] pushViewController:rootViewController animated:NO];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [WBNavigationController sharedInstance];
[self.window makeKeyAndVisible];
2.注册ViewController到全局导航栏。
// 定义快速注册viewcontroller的宏
#undef WB_IMPLEMENT_LOAD
#define WB_IMPLEMENT_LOAD( url ) \
+ (void)load { \
@autoreleasepool { \
[WBNavigationController registerWithUrl:url viewControllerClass:[self class]]; \
} \
}
#import "SecondViewController.h"
// 注册
@implementation SecondViewController
WB_IMPLEMENT_LOAD(URL_SECOND_VC)
3.跳转调用
#import "FirstViewController.h" // 不需要引用目标ViewController,此处是主调方的。
// 简单调用,不需要传递参数与回调
[self wb_pushSimpleViewController: URL_SECOND_VC];
// 完全调用
[self wb_pushViewController:^(WBNode *node) {
node.url = URL_SECOND_VC;
// node.animate = NO;
node.params = @{@"params": @"push data"};// 参数传递
node.replyAction = ^(id result) { // 回调
NSLog(@"result >> %@", result[@"result"]);
};
}];
#import "SecondViewController.h"
// 获取从前页面传递来的参数
if( self.wb_params ) NSLog(@"push get params >> %@", self.wb_params[@"params"]);
// 触发前页面的回调
if ( self.wb_replyAction ) {
self.wb_replyAction(@{@"result": @"pop return data"});
}
至此已完成了解决UIViewController之间的耦合问题。现在我们来对比一下前后代码对照:
#import "FirstViewController.h"
// 优化前
FirstViewController *controller = [[FirstViewController alloc] init];
[self.navigationController pushViewController:controller animated:YES];
// 优化后
[self wb_pushSimpleViewController: URL_FIRST_VC];
// 优化后所有ViewController的头文件应该都是这样,清爽无比。
@interface FirstViewController : UIViewController
@end
以上因为跳转ViewController间已不存在任何依赖,调用简洁清晰,更有利于项目的模块化。demo已上传至github,有任何错误与建议可以评论指出。