获取当前屏幕显示的viewcontroller

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取当前屏幕显示的viewcontroller相关的知识,希望对你有一定的参考价值。

//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}

 

以上是关于获取当前屏幕显示的viewcontroller的主要内容,如果未能解决你的问题,请参考以下文章

UI基础 获取当前屏幕显示的viewcontroller

iOS开发——获取当前屏幕显示的viewcontroller

获取当前显示的ViewController

iOS开发 - 获取当前View最顶层的ViewController

iOS开发 - 获取当前View最顶层的ViewController

获取当前控制器