从app delegate呈现视图控制器

时间:2013-10-18 06:21:23

标签: ios objective-c uiviewcontroller appdelegate

我正在尝试使用以下代码从app delegate呈现视图控制器:

- (void)interstitialViewControllerRequestSucceeded:(UIViewController *)interstitialViewController
{
    [self.window.rootViewController presentViewController:(UIViewController *)interstitialViewController animated:YES completion:NULL];
}

它将在初始视图控制器上显示插页式广告,但不显示其他插页式广告。我想让它显示在连接到导航控制器的每一个上。

如何修改此代码以实现该目标?

3 个答案:

答案 0 :(得分:48)

您也可以尝试:

[[[UIApplication sharedApplication] keyWindow] rootViewController]

我如何使用它:

#define ROOTVIEW [[[UIApplication sharedApplication] keyWindow] rootViewController]
[ROOTVIEW presentViewController:interstitialViewController animated:YES completion:^{}];

答案 1 :(得分:10)

Swift 3版本用于检查响应:

UIApplication.shared.keyWindow?.rootViewController

答案 2 :(得分:1)

快速5和更高版本

检查:

UIApplication.shared.windows.first?.rootViewController