Reputation: 420
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 ;
Upvotes: 1
Views: 967
Reputation: 531
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];
Upvotes: 2