Search code examples
iosuiwebviewuinavigationcontrolleruiwindow

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


Currently, I have set up a log-in view for users of my app. Below is the code that presents this log-in view to the user:

 // 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];
    }

What I'm attempting to do now is display a web-view first if the user has just updated the app. Below is what this new code looks like:

// 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];
    }

However, when I run the app, the web view is not displayed, I get a warning of:

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

Can anyone help me diagnose the problem? Thank you!


Solution

  • The problem was that I didn't call self.navigationController dismissViewControllerAnimated:YES completion:^

    After correcting this (see listing below), my WebView was shown as expected.

    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];
            }];
        }