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