1

I am making demo in which I have to remove some of viewControllers from the NavigationController, and for that I have implemented below code but it give me issue.

I have pushed VC1,VC2,VC3 and now I want to push VC4 and remove VC2...

ViewController4 *VC4=[[ViewController4 alloc]initWithNibName:@"ViewController4" bundle:nil];
[self.navigationController pushViewController:VC4 animated:YES];

NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];

for(UIViewController *objVC in viewControllers)
{
    if([objVC isKindOfClass:[ViewController2 class]])
    {
        [viewControllers removeObjectIdenticalTo:objVC];
    }
}
self.navigationController.viewControllers =viewControllers ;

This code works fine with iOS8 but in iOS7 with VC2 also VC3 removes automatically when I press the back button in VC4. Even if I put below code the controller automatically removes from stack.

ViewController4 *VC4=[[ViewController4 alloc]initWithNibName:@"ViewController4" bundle:nil];
[self.navigationController pushViewController:VC4 animated:YES];

NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];

self.navigationController.viewControllers =viewControllers ;
8
  • Question 1: Do you want to remove VC2 when you are pushing VC4 or when you are coming back from VC4? Commented Mar 30, 2015 at 4:36
  • Question 2: When you come back from VC4, VC2 & VC3 are removed from the stack, am I right? Commented Mar 30, 2015 at 4:37
  • After you are pushing VC4, can you log all the view controllers in the navigation stack and check if VC4 there in the stack? Commented Mar 30, 2015 at 4:42
  • Ans1 : I want to remove VC2 after pushing the VC4. Ans2: Yes, when I press back from VC4 ,I am directly come to VC1 as I removed VC2 and VC3 removes automatically. Ans3: After pushing VC4 and removing VC2 I can get desired stack VC1,VC3,VC4 in NSLog. even in viewWillAppear of VC4 NSLog the same ,but in viewDidAppear of VC4 it prints VC1,VC4 only. Commented Mar 30, 2015 at 5:15
  • Thanks! Could also check my third comment, to log the view controllers before you perform removal operation Commented Mar 30, 2015 at 5:17

1 Answer 1

2

Here is the fix, working fine in iOS7 and iOS8:

NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[self navigationController] viewControllers]];

    // Find the things to remove
        NSMutableArray *toDelete = [NSMutableArray array];

    for(UIViewController *objVC in viewControllers)
    {
        if([objVC isKindOfClass:[ViewController2 class]])
        {
            [toDelete addObject:objVC];
        }
    }
    [viewControllers removeObjectsInArray:toDelete];
    self.navigationController.viewControllers =viewControllers ;

ViewController4 *VC4=[[ViewController4 alloc]initWithNibName:@"ViewController4" bundle:nil];
[self.navigationController pushViewController:VC4 animated:YES];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.