【问题标题】:Fix Warning: Attempt to present ViewController on NavigationController whose view is not in the window hierarchy修复警告:尝试在其视图不在窗口层次结构中的 NavigationController 上呈现 ViewController
【发布时间】:2015-03-25 00:41:41
【问题描述】:

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

 // 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!

谁能帮我诊断问题?谢谢!

【问题讨论】:

  • 我实际上在做一个没有故事板的旧项目(一切都以编程方式完成)。

标签: ios uiwebview uinavigationcontroller uiwindow


【解决方案1】:

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

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

self.window.rootViewController = self.ViewController;

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

【讨论】:

  • 嗨尼克(我的名字也是!),感谢您的回复。我添加了那条线,但得到了相同的结果。我想知道是不是因为这段代码在我的 AppDelegate.m 中?
  • 你为root设置了哪个控制器?我以 ViewController 为例。尝试将 wvc 设置为您的 rootView 控制器(这就是您想要的,对吧)
  • 是的,我确实将 wvc 设置为 rootViewController,但 Xcode 仍然告诉我它的视图不在视图层次结构中。
  • 我添加了这一行来查看rootViewController的窗口是什么,并得到了(null)的日志: NSLog(@"%@", self.window.rootViewController);
【解决方案2】:

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

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

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 2015-09-17
      相关资源
      最近更新 更多