0
votes

I'm trying to pop to a view controller in my navigation stack.

This is what my storyboard looks like.

enter image description here

My table view controller has 4 cells. When I click on the fourth cell I'd like to pop to the last view controller.

#import "TableViewController.h"
#import "ViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    cell.textLabel.text = [NSString stringWithFormat:@"Cell %ld", (long)indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    switch (indexPath.row) {
        case 0:
            break;
        case 1:
            break;
        case 2:
            break;
        case 3:
            NSLog(@"%@", [NSString stringWithFormat:@"Cell %li tapped", (long)indexPath.row]);

            NSLog(@"%lu", (unsigned long)self.navigationController.viewControllers.count);

            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            ViewController *VC4 = [storyboard instantiateViewControllerWithIdentifier:@"VCFour"];
            [self.navigationController popToViewController:VC4 animated:NO];

            break;
    }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

Error

2014-11-13 13:40:58.942 Test[18601:1828547] Cell 0 tapped
2014-11-13 13:40:58.942 Test[18601:1828547] 1
2014-11-13 13:40:58.943 Test[18601:1828547] *** Assertion failure in -[UINavigationController popToViewController:transition:], /SourceCache/UIKit_Sim/UIKit-3318/UINavigationController.m:5568
2014-11-13 13:40:58.945 Test[18601:1828547] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to get popped view controller.'

As you can see the self.navigationController.viewControllers.count print 1 to the console and maybe this is the issue. Should this not be 5 as there are a total of 5 view controllers on the storyboard?

Can anyone help?

4
You can't pop to a view controller that isn't in your navigation stack. You want to PUSH a new view controller.Stonz2
How do I add those controller to my navigation stack?user3626407

4 Answers

1
votes

Your navigation stack only contains controllers already pushed on to it (this only happens when a segue is actually run). It doesn't matter if you've set them up in a story board. If you are on controller 1 and you want to get to controller 4 you can either push controller 4 next via self.navigationCotnroller pushViewController: (or by running a segue) or you can set a flag on your controller and push controller 2 which will then check the flag and push controller 3 which will check the flag and push controller 4. This way is better as you can then pop back to controllers 2/3 if you need to and your stack is as you designed.

0
votes

When you use instantiateViewControllerWithIdentifier you create a new viewcontroller, you do not get a reference to the existing one. Hence you cannot pop to it.

You seem to assume that at the time the cell is tapped, the whole viewcontroller hierarchy is already loaded, this is not the case. Only viewcontrollers that are needed are loaded, the other ones remain unused.

0
votes

First of all, the storyboard setup you have is wrong, for what you are trying to achieve. A quick and dirty solution without fixing the Storyboard navigation you have made is to replace this line: [self.navigationController popToViewController:VC4 animated:NO]; with this line: [self.navigationController pushViewController:VC4 animated:NO];

I suggest that you read Apple's Storyboard and UInavigationController docs and then try to understand why the storyboard setup you have isn't the right thing for the job.

0
votes

Hope can help you. LJMyCamViewController*lmyCamVC=self.navigationController.childViewControllers[2]; lmyCamVC.myCamera=user.SelectCamDevice; lmyCamVC.myCameraSN=user.selectedSN;

        lmyCamVC.hidesBottomBarWhenPushed = YES;

        **NSLog(@"%@------%ld",self.navigationController.childViewControllers,self.navigationController.childViewControllers.count);
        [self.navigationController popToViewController:lmyCamVC animated:NO];**