'Tried to pop to a view controller that doesn't exist.'

前端 未结 11 781
清酒与你
清酒与你 2021-02-05 12:13

I am getting this error when I call my method dismissView. Here is the method stub:

-(IBAction)dismissView
{
    RootViewController *rootController = [[RootViewC         


        
相关标签:
11条回答
  • 2021-02-05 12:27

    In my case the problem was that I was in the root ViewController and this works only for VCs that are stacked over it. To pop to the RootViewController use

    navigationController?.popToRootViewController(animated: true)
    
    0 讨论(0)
  • 2021-02-05 12:30

    If you are using Storyboads, use this segue:

    #import "PopToControllerSegue.h"
    
    @implementation PopToControllerSegue
    
    - (void) perform
    {
        UIViewController *sourceViewController = (UIViewController *)self.sourceViewController;
        UIViewController *destinationViewController = (UIViewController *)self.destinationViewController;
    
        for (UIViewController* controller in sourceViewController.navigationController.viewControllers) {
            if ([controller isKindOfClass:destinationViewController.class]) {
                [sourceViewController.navigationController popToViewController:controller animated:YES];
                return;
            }
        }
    
        NSLog(@"PopToControllerSegue has failed!");
    }
    
    @end
    
    0 讨论(0)
  • 2021-02-05 12:33

    When using Push Segues you can easily go back to the root using this method:

    [self.navigationController popToRootViewControllerAnimated:YES];
    

    When using Modal Segues (because of the word dismiss in the question and as a general reference) you can dismiss the view controller using this method:

    [self dismissViewControllerAnimated:YES completion:nil];
    
    0 讨论(0)
  • 2021-02-05 12:37

    I had this problem recently and solved with something like this...

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
    
    0 讨论(0)
  • 2021-02-05 12:47

    You're allocating the RootViewController right there. It does not exist in the navigation controller's stack, so no matter how far you pop, you won't reach it.

    0 讨论(0)
  • 2021-02-05 12:50

    Try this one line solution.

    Swift 4+:

    self.navigationController?.popToViewController ((self.navigationController?.viewControllers[1]) as! Your_ViewController, animated: true)
    

    Objective-C:

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
    
    0 讨论(0)
提交回复
热议问题