Search code examples
objective-cioscocoa-touchios6

Attempt to present * on * whose view is not in the window hierarchy


I'm trying to make a modal view controller in my app delegate (I created a function called showLoginView). But whenever I try to call it I get a warning in XCode:

Warning: Attempt to present <PSLoginViewController: 0x1fda2b40> on <PSViewController: 0x1fda0720> whose view is not in the window hierarchy!

Here's the method code:

- (void)showLoginView
{
    PSLoginViewController *loginViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:NULL] instantiateViewControllerWithIdentifier:@"PSLoginViewController"];
    [self.window.rootViewController presentViewController:loginViewController animated:NO completion:nil];
}

How can I add the view to the window hierarchy? Or maybe I'm doing something very wrong?


Solution

  • You can't display a modal view controller from the appDelegate. You need to display a modal ViewController from whichever viewController is currently displaying full-screen. In other words, you need to put that code into your root view controller, or whichever one you want to display the modal vc from...

    Also, you'll want to use the method "presentModalViewController" to present the modal. You can set properties on the modal vc such as:

    vC.modalPresentationStyle = UIModalPresentationFormSheet;
    vC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:vC animated:YES];