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

iOS 获取当前正在显示的ViewController

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

我们在非视图类中想要随时展示一个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” 的相关文章

NSButton——设定背景图片Scale

NSButton——设定背景图片Scale

参考资料: http://stackoverflow.com/questions/5851923/how-to-scale-axes-independently-at-runtime-on-an-nsbutton 代码方式: NSButton *someButton; NSButtonCell...

Proteus:Simulation is not running in real time解决方法

Proteus:Simulation is not running in real time解决方法

  Simulation is not running in real time due to excessive CPU load 如上图所示,相信这个问题很多人都碰到了。  网上说:原因是电脑CPU运行的速度跟不上单片机仿真的速度。解决方法:① ...

SVN修改Log信息

SVN修改Log信息

SVN默认是不能修改Log的,强行修改会报错:Repository has not been enabled to accept revision propchanges,ask the administrator to create a pre-revprop-change hook 如果需要修改...

Quartus II 破解器 在win7无法运行,停止工作

Quartus II 破解器 在win7无法运行,停止工作

参考资料: http://zhidao.baidu.com/link?url=6-4yMJ7njWfCK7grjNaR-EspkM9MqtpoFB5HYFa5yJK7f4sbLrMMXSd8kruGlZnmLZtP9AYQFvD70hEmR8ZV3K 现象如图所示,只要一破解,就会弹出“...

win7升级win10提示丢失api-ms-win-core-libraryloader-|1-1-1.dll

win7升级win10提示丢失api-ms-win-core-libraryloader-|1-1-1.dll

Win7 64bit 从win7升级到win10,到98%的时候提示说丢失.dll 解决办法: 用C:\Windows\System32\wimgapi.dll替换C:\Windows10Upgrade\wimgapi.dll即可 然后重新升级(由于相关文件已经下载好了,所以重新升级还是比较快的)...

ios ble:didDiscoverPeripheral没有回调

参考资料: http://stackoverflow.com/questions/26320578/diddiscoverperipheral-not-been-called-after-advertising-another-ios-device 问题: 启动scan之后,didDiscoverP...

发表评论

访客

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