ios - 修复警告 : Attempt to present ViewController on NavigationController whose view is not in the window hierarchy

标签 ios uiwebview uinavigationcontroller uiwindow

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

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

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

// 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 View ,我收到以下警告:

警告:尝试在其 View 不在窗口层次结构中的 NavigationController 上呈现 ViewController!

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

最佳答案

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

另一件应该有帮助的事情是设置你的 Root View Controller 如下:

self.window.rootViewController = self.ViewController;

编辑:因为您提到您不使用 Storyboard ,设置 rootViewController 应该可以解决问题。

关于ios - 修复警告 : Attempt to present ViewController on NavigationController whose view is not in the window hierarchy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28152881/

相关文章:

ios - UINavigationController 未返回

ios - 如何将一个 ViewController 从左侧推到另一个

ios - 推送到另一个 ViewController 时隐藏 NavigationBar

objective-c - 更改 UIBarButtonItem 的宽度

ios - Swift 中的 CoreWLAN

ios - 打开事件而不显示 UIActivityViewController

html - CSS 边框半径不适用于 UIwebview

ios - 条件绑定(bind)的初始化程序必须具有可选类型,而不是 'String' - ios - swift

ios: 将 AVPlayerLayer 移动到全屏

ios - 如何根据可变高度 UITableView 内的内容确定 UIWebView 高度?