Ricardo Ricardo - 3 months ago 11
Objective-C Question

Pop to particular controller on iOS | Objective-c

I have the following code to push new ViewControllers.

- (IBAction)btnEditPressed:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"contactListViewController"];
[self parentShowViewController:controller sender:sender];

- (void)parentShowViewController:(UIViewController *)controller sender:(id)sender {
if ([self isIOS7]) {
// En iOS7 no existe el m├ętodo showViewController
[self.navigationController pushViewController:controller animated:YES];
} else {
//[self.navigationController pushViewController:controller animated:YES];
[super showViewController:controller sender:sender];

Now I have the following scenario: I have 3 ViewControllers called A,B,C.

A->B->C If press back button I want to back from C to A


Try something like this

If you want go to Controller A from controller C on Controller C create custom back button and set the action of it, and put the following code.

 [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];

This is only work if you know that Controller A is your first Controller in Navigation.

If you don't know the order of viewController try this one

    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[ViewControllerA class]]) {
            [self.navigationController popToViewController:VC animated:Yes];

And to add a custom button go to this link

Hope this will help you.