ios 如何找到根 UIViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5968703/
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 19:52:26  来源:igfitidea点击:

How to find root UIViewController

iphoneobjective-ciosipad

提问by jamone

I have a view that is UIViewController(root) that handles ads and a UINavigationController. In that UINavigationController I have typical layers of controllers. In my custom settings table view controller, I have a button to contact me for support. When the user clicks this button I create a MFMailComposeViewControllerand would like to present it. I can't present it from the settings table view as it will be underneath my ads, so I need to reference the root view and present it from there. I've tried self.parentViewController.parentViewControllerwhere self is the settings table view, but that doesn't work. How should I reference this. It seems like a bad design to have to reference the root view directly and pass it to the settings view.

我有一个UIViewController处理广告和UINavigationController. 在那个 UINavigationController 中,我有典型的控制器层。在我的自定义设置表视图控制器中,我有一个按钮可以联系我寻求支持。当用户单击此按钮时,我会创建一个MFMailComposeViewController并希望呈现它。我无法从设置表视图中呈现它,因为它将在我的广告下方,因此我需要引用根视图并从那里呈现它。我试过self.parentViewController.parentViewControllerself 是设置表视图的地方,但这不起作用。我应该如何引用这个。必须直接引用根视图并将其传递给设置视图似乎是一个糟糕的设计。

回答by Till

Get the current keyWindow:

获取当前的keyWindow:

UIWindow *window = [UIApplication sharedApplication].keyWindow;

Get its rootViewController:

获取它的 rootViewController:

UIViewController *rootViewController = window.rootViewController;

NOTE: If an UIAlertViewis currently being shown, a new window is being created and assigned to be the keyWindow. There might be other exceptional cases as well that will not keep your application window to be the keyWindow.

注意:如果UIAlertView当前正在显示一个,则正在创建一个新窗口并将其指定为 keyWindow。可能还有其他例外情况不会让您的应用程序窗口成为 keyWindow。

回答by Mustafa

In my experience, this is the ideal solution to get the rootViewController:

根据我的经验,这是获得以下内容的理想解决方案rootViewController

[[[[UIApplication sharedApplication] delegate] window] rootViewController]

[[[[UIApplication sharedApplication] delegate] window] rootViewController]

Note: In-case of an active UIAlertor UIActionSheet, you get the Alert or Action Sheet as your key window.

注意:在活动UIAlert或的情况下UIActionSheet,您将获得警报或操作表作为关键窗口。

回答by Jesse Naugher

Use the app singleton. Something like:

使用应用单例。就像是:

[[[UIApplication sharedApplication] delegate] rootViewController]should get it if your viewController that is the root is named rootViewController

[[[UIApplication sharedApplication] delegate] rootViewController]如果作为根的 viewController 被命名,应该得到它 rootViewController

回答by Esqarrouth

You can always solve this with 1 line of code but I recommend this Swift way to do it, you can call this from anywhere, its also crash and bug safe:

你总是可以用 1 行代码来解决这个问题,但我推荐这种 Swift 方式来做到这一点,你可以从任何地方调用它,它也是崩溃和错误安全的:

/// EZSwiftExtensions - Gives you the VC on top so you can easily push your popups
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 onnoweb

Get the UIApplication object. Cycle through the windows array to find the keyWindow. And then get the rootViewController property.

获取 UIApplication 对象。循环遍历 windows 数组以查找 keyWindow。然后获取 rootViewController 属性。