36
votes

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?

7

7 Answers

31
votes

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];
21
votes

You can actually present a modal view Controller from the AppDelegate as long as you detect the current visible viewController and take care of the case where you current controller is a navigationController.

Here is what I do:

UIViewController *activeController = [UIApplication sharedApplication].keyWindow.rootViewController;
if ([activeController isKindOfClass:[UINavigationController class]]) {
   activeController = [(UINavigationController*) activeController visibleViewController];
}
[activeController presentModalViewController:loginViewController animated:YES];
8
votes
UIViewController *activeController = [UIApplication sharedApplication].keyWindow.rootViewController;
if ([activeController isKindOfClass:[UINavigationController class]])
{
   activeController = [(UINavigationController*) activeController visibleViewController];
}
else if (activeController.modalViewController)
{
    activeController = activeController.modalViewController;
}
[activeController presentModalViewController:vc animated:YES];
7
votes

I ran into this problem on iOS 7 - the key to making any of the proposed solutions work was to call

[self.window makeKeyAndVisible];

in your AppDelegate. After that call, presenting a modal view from the window's rootViewController worked.

6
votes

Another reason for that warning can be that you want to present a view controller from an instance which is not the top most view controller.

So first you have to get the topmost UIViewController and using this instance to call presentViewController:

UIViewController *root = [UIApplication sharedApplication].keyWindow.rootViewController;
while (root.presentedViewController) {
    root = root.presentedViewController;
}
3
votes

You can NSLog(@"%@", self.window.rootViewController), and see what the rootViewController really is.

I came into this problem, when the rootViewController is a normal UIViewController. Replace it with a UINavigationController, wish it will help.

0
votes

Faced this issue while trying to present controller from the call of delegate of other controller . i.e : show search filter with delegate , once done back to my controller and receive data via the delegate then present controller , all I had to do is to dispatch the present code cause while in a delegate you're in another thread , that's why you're presenting on your view from main thread another controller from that other thread , so have to go back to main thread , just put the presenting code like this :

     dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:searchVC animated:true completion:nil];
 });

Hope this helps !