获取当前显示的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的主要内容,如果未能解决你的问题,请参考以下文章

获取当前屏幕显示的viewcontroller

获取当前显示的ViewController

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

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

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

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