Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: attempt to present ViewController whose view is not in the window hierarchy

In one of my apps, I'm calling a viewController from the application didReceiveLocalNotification method. The page loads successfully, but it shows a warning as :

 Warning: Attempt to present <blankPageViewController: 0x1fda5190> on 
 <ViewController: 0x1fd85330> whose view is not in the window hierarchy!

My code is as follows :

 -(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

    blankPageViewController *myView = [[blankPageViewController alloc] 
               initWithNibName:@"blankPageViewController" bundle: nil];
    myView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.viewController presentViewController:myView animated:NO completion:nil];  
}
like image 799
Vinoy Alexander Avatar asked Mar 08 '13 05:03

Vinoy Alexander


People also ask

What is a UIViewController?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.


3 Answers

Finally I solved that issue with the following code.

-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
       self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
       self.blankviewController = [[blankPageViewController alloc] initWithNibName:@"blankPageViewController" bundle:nil];
       self.window.rootViewController = self.blankviewController;
       [self.window makeKeyAndVisible];
}
like image 56
Vinoy Alexander Avatar answered Nov 04 '22 09:11

Vinoy Alexander


Put

[self.viewController presentViewController:myView animated:NO completion:nil];  

into a function e.g.

- (void)yourNewFunction
{
    [self.viewController presentViewController:myView animated:NO completion:nil];
}

and then call it like this:

[self performSelector:@selector(yourNewFunction) withObject:nil afterDelay:0.0];

The problem got described here and why does this performSelector:withObject:afterDelay fix this problem? Because the selector will not be called until the next run of the run loop. So things have time to settle down and you will just skip one run loop.

like image 30
hashier Avatar answered Nov 04 '22 09:11

hashier


As per my assumption, I am feeling like you are trying to present myView from self.viewController before self.viewController is attached or placed in window hierarchy. So just make sure to present myView after self.viewController gets appear/attached to window.

Why can't a modal view controller present another in viewDidLoad?

like image 14
βhargavḯ Avatar answered Nov 04 '22 10:11

βhargavḯ