förschter
förschter

Reputation: 860

back button doesn't go back

After pushing a ViewController using this code

UIViewController *vc = [[self storyboard] instantiateViewControllerWithIdentifier:@"frontCardViewController"];
[self.navigationController pushViewController:vc animated:YES];

I remove all ViewControllers I don't need anymore using this code

NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
NSArray *array = [[NSArray alloc] initWithArray:navigationArray];
for (UIViewController *viewController in array) {
    if ([viewController isKindOfClass:[RITCardViewController class]]) {
            RSLog(@"Is kind of Class RITCardViewController");
            [navigationArray removeObject:viewController];
    }
}
self.navigationController.viewControllers = navigationArray;

The navigation array now looks like this:

"RITMainViewController: 0x10d81fc1>",
"RITDetailViewController: 0x10d847880",
"RITTestResultViewController: 0x113d0e090"

But the problem is that if the back button in the navigation bar is pressed now, it goes back to the second screen. But when the back button is pressed again it just stays on this screen. It seems to go trough all the screens I have removed, but doesn't show them.

What am I doing wrong?

Upvotes: 0

Views: 84

Answers (1)

bsarrazin
bsarrazin

Reputation: 4040

Try something like this instead:

NSMutableArray *viewControllers = [@[] mutableCopy];
for (UIViewController *vc in self.nagivationController.viewControllers)
{
    if (NO == [vc isKindOfClass:[RITCardViewController class]])
        [viewControllers addObject:vc];
}
self.navigationController.viewControllers = viewControllers;

Make sure you don't corrupt the stack by removing the view controller you are currently on from the view controllers array. Assuming your current view controller is not an instance of RITCardViewController you should be fine. Otherwise, you'll have to make up for it in your code.

Upvotes: 3

Related Questions