美文网首页iOS Developer
完全剪断导航栏跳转时ViewController之间的耦合

完全剪断导航栏跳转时ViewController之间的耦合

作者: 东篱先生_ | 来源:发表于2017-05-26 15:55 被阅读0次
#import "FirstViewController.h"

FirstViewController *controller = [[FirstViewController alloc] init]; 
[self.navigationController pushViewController:controller animated:YES];

上面这段代码是ios开发中很常见的一段代码,但是这平常无奇的代码却有一个隐患,这个隐患在随项目不断扩展会越来越严重。那就是
ViewController之间是存在耦合的,想要跳转目标ViewController,则必须引入对应的类头文件。更有甚者,在ViewController的.h文件中暴露属性和方法,简直无法直视。这次主要解决的问题就是彻底剪断ViewController之间的耦合,清理ViewController的.h文件中暴露的内容,还一个清爽的ViewController。

流程图
导航栏流程图.png
自定义全局导航栏
  • 初始化导航栏
    因为需要统一对目标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];
}
  • 测试调用
  1. 在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,有任何错误与建议可以评论指出。

相关文章

  • 完全剪断导航栏跳转时ViewController之间的耦合

    上面这段代码是ios开发中很常见的一段代码,但是这平常无奇的代码却有一个隐患,这个隐患在随项目不断扩展会越来越严重...

  • iOS导航栏隐藏透明处理

    1: 导航栏隐藏之间跳转 & 导航栏隐藏后显示自定义导航栏(由于系统导航栏透明造成的动画问题 暂时还未找到...

  • iOS 导航栏隐藏和显示

    1: 导航栏隐藏、显示之间跳转iOS 导航栏对于隐藏造成的动画 可以通过在 viewWillAppear 和 vi...

  • 导航栏,页面跳转

    导航栏 //ViewController.swift //UINavigationController // //...

  • iO 关于NavigationBar、NavigationIte

    前言:经常遇到这样的需求:从有导航栏的界面跳转到导航栏透明的界面,由于iOS从有导航栏跳转到透明导航栏界面,并且设...

  • 存在导航栏时iOS状态栏Style控制

    当存在导航栏(NavgationBar)时,在ViewController中控制状态栏Style不起作用,此时需要...

  • SwiftUI页面跳转

    NavigationView导航栏,NavigationLink页面跳转,navigationBarTitle导航...

  • iOS开发-模态视图跳转

    iOS开发中界面的跳转一般都会使用导航栏UINavigationController来进行push跳转,使用导航栏...

  • 导航栏平滑过渡

    在开发过程中,经常遇到导航栏隐藏的需求,当导航栏隐藏界面跳转以及返回有导航栏界面时,需要一个平滑的过渡效果,上代码:

  • 特大新闻

    导航库React Navigation 功能:1.跳转页面 2.底部导航栏 3.顶部导航栏...

网友评论

    本文标题:完全剪断导航栏跳转时ViewController之间的耦合

    本文链接:https://www.haomeiwen.com/subject/vefxfxtx.html