I'm trying to pop to a view controller in my navigation stack.
This is what my storyboard looks like.
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?