我正在制作演示,我必须从NavigationController中删除一些viewControllers,为此我已经实现了下面的代码,但它给了我一个问题。
我已经推送了VC1,VC2,VC3,现在我想推送VC4并删除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 ;
此代码适用于iOS8,但在带有VC2的iOS7中,当我按下VC4中的后退按钮时,VC3也会自动删除。 即使我在下面放置代码,控制器也会自动从堆栈中删除。
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 ;
答案 0 :(得分:2)
这是修复,在iOS7和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];