简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM