JLChen
2021-04-30 a5247b61d585627a1a7b1e1f35f34de9f0af9fba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
//  Copyright © 2018 jm. All rights reserved.
//
 
#import "UINavigationController+dh.h"
#import <LCBaseModule/DHContainerVC.h>
 
@implementation UINavigationController (dh)
- (nullable  UIViewController *)dh_topViewController
{
    UIViewController *vc = [self topViewController];
    if([vc isKindOfClass:[DHContainerVC class]])
    {
        DHContainerVC *containerVC = (DHContainerVC *)vc;
        return containerVC.contentViewController;
    }
    return vc;
}
 
+ (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [UINavigationController _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [UINavigationController _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}
 
+ (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}
 
@end