有条件地用UINavigatonController在iOS 5应用程序中跳过一个UIViewController
在我们的iOS应用程序中,有三个UIViewController
一个接一个,我们想跳过中间的一个条件,直接从第一个到第三个。 但是,用户应该能够通过第三个控制器上的“返回”button回到第二个。
我试过[self performSegueWithIdentifier:@"segueId" sender:sender];
从viewDidLoad
, viewWillAppear
但这会破坏导航栏,如debugging日志所示。 从viewDidAppear
调用此代码工作正常,但第二个视图已经显示,这正是我想要避免的第一个地方。
我也试过[self.navigationController pushViewController:vc animated:NO];
但结果是同样损坏的导航栏,即使这次debugging日志没有这样的条目。
这样做的支持方式是什么(如果可能的话)?
目标是使用iOS 5.1的iPhone4,开发环境是Xcode 4.3。
我在应用程序中使用下面的代码。 完全按照预期工作。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { SecondViewController *secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"]; if (indexPath.row == 0) { // skip second vc ThirdViewController *thirdVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ThirdViewControllerViewController"]; [self.navigationController pushViewController:secondVC animated:NO]; [self.navigationController pushViewController:thirdVC animated:YES]; } else { // push second vc [self.navigationController pushViewController:secondVC animated:YES]; } }
如果你想跳过一个视图控制器,你可以调用UINavigationController setViewControllers:animated:
它将animation到提供的数组中的最后一个控制器,用户将能够“退出”该堆栈。
你可以用你喜欢的方式build立视图控制器的数组; 也许从现有的视图控制器数组开始:
NSMutableArray* newViewControllers = [[navController.viewcontrollers mutablecopy] autorelease]; [newViewControllers addObject: ...]; [navController setViewControllers: newViewControllers animated: YES];