Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally skipping a UIViewController in an iOS 5 app with UINavigatonController

In our iOS application with three UIViewControllers 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.

like image 951
alokoko Avatar asked Mar 13 '12 21:03

alokoko


2 Answers

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];
    }
}
like image 137
Matthias Bauch Avatar answered Nov 19 '22 16:11

Matthias Bauch


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];
like image 44
TomSwift Avatar answered Nov 19 '22 18:11

TomSwift