当前位置:首页 > 技术心得 > 正文内容

iOS 获取当前正在显示的ViewController

xjtudll11年前 (2015-08-16)技术心得10180

我们在非视图类中想要随时展示一个view时,需要将被展示的view加到当前view的子视图,或用当前view presentViewController,或pushViewContrller,这些操作都需要获取当前正在显示的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;
}

参考资料:


扫描二维码推送至手机访问。

版权声明:本文由鸟的天空发布,如需转载请注明出处。

本文链接:http://xjtudll.cn/Exp/364/

标签: ios
分享给朋友:

“iOS 获取当前正在显示的ViewController” 的相关文章

android bluetoothGatt  :onClientRegistered() - status=133 clientIf=0

android bluetoothGatt :onClientRegistered() - status=133 clientIf=0

参考资料: http://stackoverflow.com/questions/25330938/android-bluetoothgatt-status-133-register-callback http://stackoverflow.com/questions/28894111/andro...

OSAL系统如何写中断服务函数

OSAL系统如何写中断服务函数

参考资料; http://bbs.21ic.com/icview-192248-1-1.html 问题: OSAL添加中断服务程序的时候编译报错 提示:Error[Pa045]: function "T4_ISR" has no prototype 但是定时器中断实验程...

Android Studio: This version of the rendering library is more recent than your version of Android Studio

Android Studio: This version of the rendering library is more recent than your version of Android Studio

Android Studio预览xml布局时,提示: This version of the rendering library is more recent than your version of Android Studio. Please update Android Studio 如图所示...

Keil查看编译后的汇编代码

Keil查看编译后的汇编代码

在使用Keil编译C51时,一般情况下,大家都不去看汇编代码。但在某些特殊情况下,查看汇编代码就是有必要了。 (1)确认C代码是否正确 (2)研究算法 查看汇编代码前的某些准备工作是必须的:你得有工程,并确定程序编译无误。在查看汇编代码前,编译一次。 编译OK后,点Debug 有可能会提示你没有...

Excel引用单元格地址的方法

单元格本身有两种表示方式: 1、列用字母,行用数字 例如:A1表示第一行第一列,A2表示第二行第一列 2、R表示行,C表示列 例如:R1C1就表示1行1列,R5C8就表示第5行第8列。 Excel单元格地址引用有两大方式:相对引用和绝对引用。 (1) 对于第一种单元格表示方式(A1) 其相对引用和绝...

Xcode查找函数(方法)调用及被调用

Xcode查找函数(方法)调用及被调用

参考资料:http://stackoverflow.com/questions/7145045/find-method-references-in-xcode 这个功能有的说是 Find Caller,有的说是Find references,有的说是Find Usages 直白的讲,就是我需要知道这...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。