Conditionally skipping a UIViewController in an iOS 5 app with UINavigatonController

半腔热情 提交于 2020-01-23 05:05:05

问题


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.


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!