在 iOS 中查找 rootViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8585881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:58:03  来源:igfitidea点击:

Finding the rootViewController in iOS

iosuiapplication

提问by Jim

In ShareKit, the code needs to determine where the rootViewController is so it can show a modal view. For some reason, the code is failing in iOS 5:

在 ShareKit 中,代码需要确定 rootViewController 的位置,以便它可以显示模态视图。出于某种原因,代码在 iOS 5 中失败了:

    // Try to find the root view controller programmically

    // Find the top window (that is not an alert view or other window)
    UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
    if (topWindow.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(topWindow in windows)
        {
            if (topWindow.windowLevel == UIWindowLevelNormal)
                break;
        }
    }

    UIView *rootView = [[topWindow subviews] objectAtIndex:0];  
    id nextResponder = [rootView nextResponder];

    if ([nextResponder isKindOfClass:[UIViewController class]])
        self.rootViewController = nextResponder;

    else
        NSAssert(NO, @"ShareKit: Could not find a root view controller.  You can assign one manually by calling [[SHK currentHelper] setRootViewController:YOURROOTVIEWCONTROLLER].");

This is hitting the assert.

这是命中断言。

What is wrong with simply using the following code, instead?

简单地使用以下代码有什么问题呢?

rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];

This seems to be working fine. Will it fail under some conditions?

这似乎工作正常。在某些条件下它会失败吗?

回答by XJones

I'm not sure if you can rely on window.rootViewControllerb/c you don't have to set it. You can just add a subview to the window. The following seemed to work fine:

我不确定你是否可以依靠window.rootViewControllerb/c 你不必设置它。您可以只向窗口添加一个子视图。以下似乎工作正常:

id rootVC = [[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] nextResponder];

回答by Esqarrouth

Swift way to do it, you can call this from anywhere:

快速的方式来做到这一点,你可以从任何地方调用它:

/// EZSwiftExtensions - Gives you the VC on top so you can easily push your popups
public var topMostVC: UIViewController? {
    var presentedVC = UIApplication.sharedApplication().keyWindow?.rootViewController
    while let pVC = presentedVC?.presentedViewController {
        presentedVC = pVC
    }

    if presentedVC == nil {
        print("EZSwiftExtensions Error: You don't have any views set. You may be calling them in viewDidLoad. Try viewDidAppear instead.")
    }
    return presentedVC
}

Its included as a standard function in:

它作为标准功能包含在:

https://github.com/goktugyil/EZSwiftExtensions

https://github.com/goktugyil/EZSwiftExtensions

回答by Pedro Trujillo

This worked for me:

这对我有用:

UIViewController * rootViewController = (UIViewController *)[[[UIApplication.sharedApplication.keyWindow subviews] firstObject];