ios 从应用程序委托呈现视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19442900/
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
Present view controller from app delegate
提问by mikeholp
I'm attempting to present a view controller from the app delegate with this code:
我正在尝试使用以下代码从应用程序委托呈现一个视图控制器:
- (void)interstitialViewControllerRequestSucceeded:(UIViewController *)interstitialViewController
{
[self.window.rootViewController presentViewController:(UIViewController *)interstitialViewController animated:YES completion:NULL];
}
It will display the interstitial on the initial view controller but none of the others. I want it to display on every one attached to a navigation controller.
它将在初始视图控制器上显示插页式广告,但不显示其他任何内容。我希望它显示在每个连接到导航控制器的控制器上。
How can I modify this code to achieve that goal?
如何修改此代码以实现该目标?
回答by WrightsCS
You can also try:
你也可以试试:
[[[UIApplication sharedApplication] keyWindow] rootViewController]
How I use it:
我如何使用它:
#define ROOTVIEW [[[UIApplication sharedApplication] keyWindow] rootViewController]
[ROOTVIEW presentViewController:interstitialViewController animated:YES completion:^{}];
回答by Kevin ABRIOUX
Swift 3 version for checked response :
用于检查响应的 Swift 3 版本:
UIApplication.shared.keyWindow?.rootViewController
回答by Jonas Deichelmann
Swift 5 and up
Swift 5 及以上
Check this:
检查这个:
UIApplication.shared.windows.first?.rootViewController