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

iOS 获取当前正在显示的ViewController

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

我们在非视图类中想要随时展示一个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 停止Handler

mHandler.removeCallbacks(mRunnable); 或者 mHandler.removeMessages(what); mHandler.removeCallbacksAndMessages(null) 参数为null的话,会将所有的Callbacks和Messages全部清除...

Android BLE:onServicesDiscovered(BluetoothGatt gatt, int status) 没有回调过

问题: .discoverServices()调用之后,却永远不回调onServicesDiscovered(BluetoothGatt gatt, int status) 基本代码如下: public void onConnectionStateChange(BluetoothGatt gatt...

OKI单片机——ML610Q4XX PWM

OKI单片机——ML610Q4XX PWM

  /***********************************************/ * @brief 初始PWM 引脚 * @details /***********************************************/ void P43_Init...

Quartus II Warning:can't generate programming files because you are currently using the Quartus II softwore in Evaluation Node

开发环境: Quartus II 11.0 问题: 代码编译通过,但是下载到CPLD,没有任何现象 且有warning 如下: can't generate programming files because you are currently using the Quartus II softwo...

PHP:Call to undefined function bindtextdomain()

all to undefined function bindtextdomain() bindtextdomain() 是系统函数为什么会出现上面情况 我的是 PHP/5.2.9 (PHP 4, PHP 5) bindtextdomain ? Set...

金蝶物料属性为自制,MRP计算需要产生委外类型的计划订单?

解决方案如下: 物料资料中设置该物料的默认生产类型为委外加工即可。具体操作步骤如下: 1、登录KIS旗舰版主控台,依次单击【系统设置】→【基础资料】→【公共资料】,双击【物料】; 2、进入物料资料界面,选择需要设置的物料点击【修改】,在弹出的【物料-修改】窗口中,选择【计划资料】...

发表评论

访客

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