修复警告:尝试在其视图不在 window 层次结构中的 NavigationController 上呈现 ViewController

Fix Warning: Attempt to present ViewController on NavigationController whose view is not in the window hierarchy

目前,我已经为我的应用程序的用户设置了登录视图。下面是向用户显示此登录视图的代码:

 // Handle how we present the view
    if (self.notificationToProcess != nil) {
        [self.navigationController dismissViewControllerAnimated:YES completion:^{
            SWNotificationsViewController *viewController = [[NotificationsViewController alloc] init];
            viewController.initialDataID = self.notificationToProcess[@"Data"];
            self.notificationToProcess = nil;

            [self.navigationController pushViewController:viewController animated:YES];
        }];
    } else if (self.detailURL != nil) {
        [self.navigationController dismissViewControllerAnimated:YES completion:^{
            WebViewController *wvc = [[WebViewController alloc] init];
            wvc.URL = self.detailURL;
            wvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            [self.navigationController presentViewController:wvc animated:YES completion:nil];

            self.detailURL = nil;
        }];
    } else {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }

我现在尝试做的是,如果用户刚刚更新了应用程序,则首先显示网络视图。下面是这个新代码的样子:

// Handle how we present the view.
    if (self.notificationToProcess != nil) {
        [self.navigationController dismissViewControllerAnimated:YES completion:^{
            SWNotificationsViewController *viewController = [[SWNotificationsViewController alloc] init];
            viewController.initialDataID = self.notificationToProcess[@"Data"];
            self.notificationToProcess = nil;

            [self.navigationController pushViewController:viewController animated:YES];
        }];
    } else if (self.detailURL != nil) {
        [self.navigationController dismissViewControllerAnimated:YES completion:^{
            SWWebViewController *wvc = [[SWWebViewController alloc] init];
            wvc.URL = self.detailURL;
            wvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            [self.navigationController presentViewController:wvc animated:YES completion:nil];

            self.detailURL = nil;
        }];
else if (![versionOfLastRun isEqual:currentVersion])
    {
        SWWebViewController *webViewController = [[SWWebViewController alloc] init];
        NSString *url=@"http://someurl";
        webViewController.URL = url;
        webViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self.navigationController presentViewController:webViewController animated:YES completion:nil];
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"VersionOfLastRun"];
        [[NSUserDefaults standardUserDefaults] synchronize];

    }
    else if (versionOfLastRun == nil){
        // First start after installing the app
    }

    else {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }

但是,当我 运行 应用程序时,网络视图未显示,我收到警告:

警告:试图在其视图不在 window 层次结构中的 NavigationController 上呈现 ViewController!

任何人都可以帮助我诊断问题吗?谢谢!

您的 viewController 似乎不在导航控制器层次结构中。这意味着您的控制器未与应处理所显示控制器的导航控制器堆栈连接。

另一件应该有帮助的事情是设置你的根视图控制器如下:

self.window.rootViewController = self.ViewController;

编辑:既然你提到你不使用故事板,设置 rootViewController 应该可以解决问题。

初始化导航控制器后,尝试将 window 的 rootViewController 设置为导航控制器:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainController];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navController;

这就是我在没有故事板的情况下的做法。

问题是我没有打电话给 self.navigationController dismissViewControllerAnimated:YES completion:^

更正此问题后(参见下面的清单),我的 WebView 已按预期显示。

else if (![versionOfLastRun isEqual:currentVersion])
    {
        [self.navigationController dismissViewControllerAnimated:YES completion:^{
            SWWebViewController *webViewController = [[SWWebViewController alloc] init];

            NSLog(@"%@", self.window.rootViewController);

            NSString *url=@"http://devstatwatch.drbsystems.com/releases_mobile.php";
            webViewController.URL = url;
            webViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            [self.navigationController presentViewController:webViewController animated:YES completion:nil];
            [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"VersionOfLastRun"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }];
    }