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

后端 未结 30 3104
一整个雨季
一整个雨季 2020-11-21 23:39

Just started using Xcode 4.5 and I got this error in the console:

Warning: Attempt to present < finishViewController: 0x1e56e0a0 > on < ViewCont

相关标签:
30条回答
  • 2020-11-22 00:12

    Another potential cause:

    I had this issue when I was accidentally presenting the same view controller twice. (Once with performSegueWithIdentifer:sender: which was called when the button was pressed, and a second time with a segue connected directly to the button).

    Effectively, two segues were firing at the same time, and I got the error: Attempt to present X on Y whose view is not in the window hierarchy!

    0 讨论(0)
  • 2020-11-22 00:12

    If other solutions does not look good for some reason, you can still use this good old workaround of presenting with the delay of 0, like this:

    dispatch_after(0, dispatch_get_main_queue(), ^{
        finishViewController *finished = [self.storyboard instantiateViewControllerWithIdentifier:@"finishViewController"];
        [self presentViewController:finished animated:NO completion:NULL];    
    });
    

    While I've seen no documented guarantee that your VC would be on the view hierarchy on the time dispatch block is scheduled to execution, I've observed it would work just fine.

    Using delay of e.g. 0.2 sec is also an option. And the best thing - this way you don't need to mess with boolean variable in viewDidAppear:

    0 讨论(0)
  • 2020-11-22 00:13

    With Swift 3...

    Another possible cause to this, which happened to me, was having a segue from a tableViewCell to another ViewController on the Storyboard. I also used override func prepare(for segue: UIStoryboardSegue, sender: Any?) {} when the cell was clicked.

    I fixed this issue by making a segue from ViewController to ViewController.

    0 讨论(0)
  • 2020-11-22 00:15

    It happened to me that the segue in the storyboard was some kind of broken. Deleting the segue (and creating the exact same segue again) solved the issue.

    0 讨论(0)
  • 2020-11-22 00:16

    For Display any subview to main view,Please use following code

    UIViewController *yourCurrentViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    while (yourCurrentViewController.presentedViewController) 
    {
       yourCurrentViewController = yourCurrentViewController.presentedViewController;
    }
    
    [yourCurrentViewController presentViewController:composeViewController animated:YES completion:nil];
    

    For Dismiss any subview from main view,Please use following code

    UIViewController *yourCurrentViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    while (yourCurrentViewController.presentedViewController) 
    {
       yourCurrentViewController = yourCurrentViewController.presentedViewController;
    }
    
    [yourCurrentViewController dismissViewControllerAnimated:YES completion:nil];
    
    0 讨论(0)
  • If you have AVPlayer object with played video you have to pause video first.

    0 讨论(0)
提交回复
热议问题