修复警告:尝试在其视图不在窗口层次结构中的NavigationController上显示ViewController

时间:2015-01-26 15:11:36

标签: ios uiwebview uinavigationcontroller uiwindow

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

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

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

警告:尝试在视图不在窗口层次结构中的NavigationController上显示ViewController!

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

3 个答案:

答案 0 :(得分:1)

好像你的viewController不在导航控制器层次结构中。这意味着您的控制器未与应处理所提供控制器的导航控制器堆栈连接。

另一件应该有用的事情是按如下方式设置根视图控制器:

self.window.rootViewController = self.ViewController;
编辑:既然你提到你不使用故事板,那么设置rootViewController应该可以解决问题。

答案 1 :(得分:0)

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

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

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

这就是我没有故事板的方式。

答案 2 :(得分:0)

问题是我没有打电话给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];
        }];
    }