iOS获取屏幕上正在显示的控制器

开发项目过程中,有需要获取当前正在展示的控制器的需求。本来想用响应链的思路来实现,发现会有很多问题。后面发现可以通过控制器的入栈方式来解决这个问题。

1. 控制器入栈方式,示例代码如下:

///获取当前活动的控制器
+ (UIViewController *)getCurrentActivityViewController {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    NSLog(@"window level: %.0f", window.windowLevel);
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for (UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    
    //从根控制器开始查找
    UIViewController *rootVC = window.rootViewController;
    UIViewController *activityVC = nil;
    
    while (true) {
        if ([rootVC isKindOfClass:[UINavigationController class]]) {
            activityVC = [(UINavigationController *)rootVC visibleViewController];
        } else if ([rootVC isKindOfClass:[UITabBarController class]]) {
            activityVC = [(UITabBarController *)rootVC selectedViewController];
        } else if (rootVC.presentedViewController) {
            activityVC = rootVC.presentedViewController;
        } else if ([rootVC isKindOfClass:[RTContainerController class]]) {
            activityVC = [(RTContainerController *)rootVC contentViewController];
        } else {
            break;
        }
        
        rootVC = activityVC;
    }
    
    return activityVC;
}

上面的代码中的RTContainerController是我们项目中用到的 RTRootNavigationController 框架,如果项目中没有用到的可以把上面对应判断项去掉。

2. 响应链的方式(有问题,思路可以参考)

/// 获取当前活动的控制器
+ (UIViewController *)getCurrentActivityViewController {
    UIViewController *result = nil;
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    NSLog(@"window level: %.0f", window.windowLevel);
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for (UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    
    //通过响应链来找到当前View的控制器
    UIView *rootVCView = window.rootViewController.view;
    UIView *lastView = [self getLastSubviewsWithView:rootVCView];
    NSLog(@"lastView = %@", lastView);
    id nextResponder = [lastView nextResponder];
    while (nextResponder) {
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            break;
        } else if ([nextResponder isKindOfClass:[UIView class]]) {
            nextResponder = [nextResponder nextResponder];
        } else if ([nextResponder isKindOfClass:[UIApplication class]]) {
            nextResponder = nil;
            break;
        }
    }
    
    if (nextResponder != nil) {
        result = nextResponder;
    } else {
        result = window.rootViewController;
    }
    
    NSLog(@"当前最前面的活动的控制器是: %@", result);
    return result;
}

/// 获取View的最底层子View
+ (UIView *)getLastSubviewsWithView:(UIView *)view {
    UIView *lastView = view;
    NSMutableString *viewString = [NSMutableString string];
    while (lastView.subviews.count != 0) {
        [viewString appendFormat:@"%@=>", NSStringFromClass(lastView.class)];
        lastView = lastView.subviews.lastObject;
    }
    [viewString appendFormat:@"%@", NSStringFromClass(lastView.class)];
    NSLog(@"查找最底层的View的过程是: %@", viewString);
    return lastView;
}

3. 参考

iOS 获得屏幕正在显示的Controller(自定义的控制器)的实用方法

你可能感兴趣的:(iOS获取屏幕上正在显示的控制器)