Warning: attempt to present ViewController whose v

2019-01-16 11:26发布

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];  
}

9条回答
▲ chillily
2楼-- · 2019-01-16 11:59

For Swift 2.0 I used an override for viewDidAppear with:

let vc = self.storyboard?.instantiateViewControllerWithIdentifier("Second") as! SecondViewController

self.presentViewController(vc, animated: true, completion: nil)

Where "Second" is storyboard ID of the SecondViewController in the identity inspector.

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-16 12:01

If your application type id UINavigationController then you can use.

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

blankPageViewController *myView = [[blankPageViewController alloc]
                                   initWithNibName:@"blankPageViewController" bundle: nil];
myView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController presentViewController:myView animated:NO completion:nil]; }

Hope this will help you. All the best !!!

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-16 12:12

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.

查看更多
登录 后发表回答