问题
In our iOS application with three UIViewController
s one after another, we would like to skip the middle one based on some condition and go directly from first to third. However, the user should be able to come back to the second one via the "Back" button on the third controller.
I tried [self performSegueWithIdentifier:@"segueId" sender:sender];
from viewDidLoad
, viewWillAppear
but this corrupts the navigation bar as indicated by the debug log. Calling this code from viewDidAppear
works fine, but then the second view is already displayed, which is what I was trying to avoid in the first place.
I also tried [self.navigationController pushViewController:vc animated:NO];
but the result is similarly corrupted nav bar, even though this time debug log does not have such entries.
What would be the supported way of doing this (if it is at all possible)?
The target is iPhone4 with iOS 5.1, and the dev environment is Xcode 4.3.
回答1:
I use the following code in an app. Works exactly as expected.
- (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];
}
}
回答2:
If you want to skip a view controller you can just call UINavigationController setViewControllers:animated:
It will animate to the last controller in the supplied array, and the user will be able to "back" out of that stack.
You can build up the array of view controllers any way you like; perhaps starting with the existing array of view controllers:
NSMutableArray* newViewControllers = [[navController.viewcontrollers mutablecopy] autorelease];
[newViewControllers addObject: ...];
[navController setViewControllers: newViewControllers animated: YES];
来源:https://stackoverflow.com/questions/9692572/conditionally-skipping-a-uiviewcontroller-in-an-ios-5-app-with-uinavigatoncontro