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.
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];
}
}
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With