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

11

16

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

-(IBAction)dismissView
{
    RootViewController *rootController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    [self.navigationController popToViewController:rootController animated:YES];
}

That should work, and I've checked, rootController is initialized and allocated. Any ideas?

Caliban answered 1/2, 2010 at 20:9 Comment(0)
N
28

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

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
Nanananak answered 16/11, 2012 at 13:49 Comment(1)
swift 3 implementation let _ = self.navigationController?.popToViewController((self.navigationController?.viewControllers[1]) as! HomeViewController, animated: true)Pepsin
G
13

The -popToViewController is used to pop view controllers OFF the stack, down to one that already exists. Your UINavigationController has a stack of ViewControllers (stored in the viewControllers property), when you popToViewController, you're going to want to pass one of the elements in that array as the first argument.

What you most likely want to do in this case is use -popViewControllerAnimated:, which will remove the top ViewController from the stack

Gyroscope answered 1/2, 2010 at 20:14 Comment(0)
K
8

Swift 4

For anyone still looking for a better solution that doesn't involve the UINavigationController stack indexes, which is getting more problematic with bigger navigation stack - here's the easiest way to solve this:

if let destinationViewController = navigationController?.viewControllers
                                                        .filter(
                                      {$0 is DestinationViewController})
                                                        .first {
    navigationController?.popToViewController(destinationViewController, animated: true)
}
Ketron answered 24/4, 2019 at 20:18 Comment(1)
Why not do $0 is DestinationViewController instead $0.classForCoder == DestinationViewController.self ?Hankhanke
S
6

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.

Spotlight answered 1/2, 2010 at 20:17 Comment(0)
P
4

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
Pitchblende answered 17/7, 2013 at 6:14 Comment(0)
S
1

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];
Segal answered 8/3, 2014 at 6:25 Comment(0)
F
1

The UINavigationController has a stack of ViewControllers which is stored in the viewControllers(NSArray) property. Enumerate to the required ViewController and pop to that ViewController.

Following code should solve the problem.

-(IBAction)dismissView
{
    NSArray *array = self.navigationController.viewControllers;
    for (id controller in array) {
        if ([controller isKindOfClass:[RootViewController class]]) {
            [self.navigationController popToViewController:controller animated:YES];

        }
    }
}
Flaunt answered 3/2, 2016 at 11:39 Comment(0)
S
0

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];
Stacistacia answered 25/2, 2019 at 15:53 Comment(0)
N
0

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)
Nikolia answered 22/8, 2019 at 9:32 Comment(0)
E
0

Before popToViewController, check that NavigationController contains your ViewController:

if navigationController.viewControllers.contains(entryViewController) {
    navigationController.popToViewController(entryViewController, animated: true)
}
Edward answered 16/2, 2024 at 4:23 Comment(0)
L
-1
self.navigationController?.present(viewControllers, animated: true, completion: nil)
Limoges answered 9/10, 2019 at 11:47 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.