代码之家  ›  专栏  ›  技术社区  ›  user2767343

警告:试图在SampleViewController上显示UINavigationController:其视图不在窗口层次结构中

  •  0
  • user2767343  · 技术社区  · 11 年前

    我已经在这个论坛上尝试了所有的解决方案,但仍然无法解决。有人能帮我解决这些问题吗?我希望应用程序在NSUserDefault从Urban Airship读取密钥@“url”时打开包含tableView的inboxData类。。

    这是来自SampleViewController类;

    -(void)viewDidAppear:(BOOL)animated{
    
        [super viewDidAppear:animated];
    
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *action_9 = [defaults objectForKey:@"url"];
    
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"url"] isEqualToString:@"aa9"])
    
    {
    
            inboxData *screen=[[inboxData alloc]initWithNibName:@"inboxData" bundle:nil];
            screen.strGetTableSelect=@"1";
            UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:screen];
            [self presentModalViewController:navigationController animated:YES];
    
        }
    }
    

    返回时出现以下错误。。

    警告:试图在视图不在窗口层次结构中的SampleViewController上显示UINavigationController

    1 回复  |  直到 11 年前
        1
  •  1
  •   karthika    11 年前

    您的SampleViewController不在Window层次结构中,您需要在窗口中进行设置。

    在你的appDelegate中,这样做,

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.viewController = [[SampleViewController alloc] initWithNibName:@"SampleViewController" bundle:nil];
        self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
        self.window.rootViewController = self.navigationController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    如果你正在使用故事板,在didFinishLaunchingWithOptions方法中这样使用,

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone"
                                                                 bundle: nil];
     
        SampleViewController *mainViewController = (SampleViewController*)[mainStoryboard
                                                           instantiateViewControllerWithIdentifier: @"SampleViewController"];
     
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
     
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        [self.window setRootViewController:navigationController];
        [self.window setBackgroundColor:[UIColor whiteColor]];
        [self.window makeKeyAndVisible];
     
        return YES;
    }
    

    在您的视图控制器中,

    -(void)viewDidAppear:(BOOL)animated{
    
        [super viewDidAppear:animated];
    
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *action_9 = [defaults objectForKey:@"url"];
    
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"url"] isEqualToString:@"aa9"])
    
    {
            inboxData *screen=[[inboxData alloc]initWithNibName:@"inboxData" bundle:nil];
            screen.strGetTableSelect=@"1";
            [self presentModalViewController:screen animated:YES];
    
        }
    }