美文网首页Window待学习
获取 APP 屏幕最上层的 View Controller(续)

获取 APP 屏幕最上层的 View Controller(续)

作者: waylen | 来源:发表于2017-02-06 13:56 被阅读1046次

我一直在想,应该还有比之前一篇文章更为简洁的方式去获取 APP 屏幕最上层的 View Controller。当一个 controller 出现在屏幕上,其生命周期的-viewDidAppear:方法会被调用,因此只需要在该方法中记录当前的 controller 即可。为了减少对原有代码的侵入性,考虑使用 Swizzing 对 -viewDidAppear: 方法挂钩。

// TopVC.h
@interface TopVC : NSObject

@property (nonatomic, weak, readonly) UIViewController *top;

+ (instancetype)shared;

@end
// TopVC.m
#define swizzing(a, b) ...
@interface TopVC ()

@property (nonatomic, weak, readwrite) UIViewController *top;

@end

@implementation TopVC

+ (instancetype)shared {
    static TopVC *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    
    return instance;
}

@end

@implementation UIViewController (VCS)

+ (void)load {
    @autoreleasepool {
        // swizzing 是自定义的一个宏,用于实现方法交换。
        swizzing(@"viewDidAppear:", @"vcs_viewDidAppear:"); 
    }
}

- (void)vcs_viewDidAppear:(BOOL)animated {
    [TopVC shared].top = self;
    [self vcs_viewDidAppear:animated];
}
@end

#undef swizzing

将上述代两个文件放入项目中,直接使用[TopVC shared].top获取即可。项目地址:https://github.com/zhwayne/TopVC.

2017-10-23 更新:
有些时候我们并不希望当前添加的 UIViewController 影响到最上层视图控制器的追踪,比如某些 ChildViewController 对用户不可见,或者 ViewController 为半透明弹窗(如 UIAlertViewController ),这时建议为 UIViewController 添加分类新增是否参与最上层控制器追踪的属性,如此即可。

以上。

相关文章

网友评论

  • 此账号不添加好友:当弹出视图的modalPresentationStyle = UIModalPresentationCustom的时候。弹出层消失的时候,不会调用对应的viewDidAppear
  • JihanWen:建议为 UIViewController 添加分类新增是否参与最上层控制器追踪的属性?能否具体点呢
    waylen:嗯,比如可以通过分类为 UIViewController 添加一个 allowMostTopTrace 属性,默认值为 YES。然后把 TopVC 的 vcs_viewDidAppear 方法改成这样:

    - (void)vcs_viewDidAppear:(BOOL)animated {
    if (self. allowMostTopTrace) {
    [TopVC shared].top = self;
    }
    [self vcs_viewDidAppear:animated];
    }
  • KooFrank:推荐把top的属性strong改为weak 不然vc释放不了
    waylen:感谢指正
  • Cerko:你这样做的话,我就不能Controller addChildController了吧?
    waylen:可以的,你需要让你的 childViewController 重新支持生命周期的方法调用,默认情况下,childViewController 的生命周期方法是不会被调用的。
  • Smicro:你这样写是有问题的。首先,你的 +load 中应该加入 dispatch_once 来进行限制。
    榆木脑袋1:@waylen 特意找了一下资料,在某些情况下可能的确是需要dispatch_once的,但是上述应该是不要的,
    waylen:@榆木脑袋1 除了手动调用 load 方法这个特例外,我不认为 dispatch_once 是必须的,load 方法由系统调用执行始于 main 方法之前,且会阻塞后续代码执行。
    榆木脑袋1:load 需要 dispatch_once???

本文标题:获取 APP 屏幕最上层的 View Controller(续)

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