Search code examples
iphoneiosback-button

Backbutton that goes back two or more Controllers


I know that overriding the back button functionality is not considered a good user design. However I have process, at which at some point going back would'nt make any sense. Instead I would like the user to go back two or more controller

So within certain ViewControllers, clicking the back-button should trigger the pop of several ViewControllers, not just the one in front. I tried around with subclassing the NavigationController and overriding the popViewController-Method:

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    if([[self.viewControllers lastObject] class] == [MyCOntroller class]){
        [super popViewControllerAnimated:NO]; // pop once

        return [super popViewControllerAnimated:animated]; // pop twice
    } else {
        return [super popViewControllerAnimated:animated];
    }
}

However I get problems, where the NavigationTopBar is not in sync anymore withe the ViewController being in front. Anybody ran into the same issues?


Solution

  • Did u try using

    popToViewController:animated:
    

    Pops view controllers until the specified view controller is at the top of the navigation stack.

    Maybe u can have custom back buttons for such view controllers and then try

    - (IBAction)backButtonPressed
    {
    [yourNavigationcontroller popToViewController:viewController animated:YES];
    }