Warning: Attempt to present UINavigationController on SampleViewController: whose view is not in the window hierarchy

StackOverflow https://stackoverflow.com/questions/19313695

  •  30-06-2022
  •  | 
  •  

i have try all the solution in this forum but still cannot solve. Can anybody help me out with these problem? i want the app to open the inboxData class that contain a tableView when the NSUserDefault read the key @"url" from Urban Airship..

this is from SampleViewController class;

-(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];

    }
}

it return with these error..

Warning: Attempt to present UINavigationController on SampleViewController whose view is not in the window hierarchy

有帮助吗?

解决方案

Your SampleViewController not in the Window hierarchy, You need to set in window.

In your appDelegate, do like this,

- (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;
}

If you are using storyboad, Use like this in didFinishLaunchingWithOptions method,

- (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;
}

In your view controller,

-(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];

    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top