Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 388
patryk Avatar asked Nov 12 '12 20:11

patryk


People also ask

What is Definespresentationcontext?

A Boolean value that indicates whether this view controller's view is covered when the view controller or one of its descendants presents a view controller.

What is Navigation Controller in IOS?

The navigation controller manages the navigation bar at the top of the interface and an optional toolbar at the bottom of the interface. The navigation bar is always present and is managed by the navigation controller itself, which updates the navigation bar using the content provided by its child view controllers.


2 Answers

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]; 
like image 177
HackyStack Avatar answered Oct 07 '22 20:10

HackyStack


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]; 
like image 42
Erwan Avatar answered Oct 07 '22 21:10

Erwan