首页 文章

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

提问于
浏览
2

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

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

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

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

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

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

    self.window.rootViewController = self.ViewController;
    

    编辑:因为你提到你不使用故事板,设置rootViewController应该做的伎俩 .

  • 0

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

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

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

  • 1

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

相关问题