Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popping multiple views from the navigation controller

I have an application which its rootview is a menu to 4 tableviews that the user uses to set up a search query by selecting a cell that loads another subview, so the basic structure looks like this

Root View
- Parent View (search view)
--Sub View (user selects variables here to fill search parameters of the parent view

But one of the Parent View search parameters requiers another sub view to be pushed onto the navigation stack so it would look like

Root View
- Parent View (search view)
--Sub View (user selects variables here to fill search parameters of the parent view
---Sub View (related values to the previous subview i.e. Model / sub model)

I would like to know if there is a way to pop back to the Parent View from this Sub View.. I know you can pop a single view or pop back to rootview but on this occasion I want to pop two subviews... is this possible?

like image 937
C.Johns Avatar asked Dec 13 '22 06:12

C.Johns


2 Answers

UINavigationViewController

popToViewController:animated:

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

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
like image 86
Daryl Teo Avatar answered Jan 05 '23 23:01

Daryl Teo


You can add a category to UINavigationController to allow multiple controllers to be popped at once.

UINavigationController+VariablePop.h

#import <UIKit/UIKit.h>

@interface UINavigationController (VariablePop)

- (NSArray *)popViewControllers:(int)numPops animated:(BOOL)animated;

@end

UINavigationController+VariablePop.m #import "UINavigationController+VariablePop.h"

@implementation UINavigationController (VariablePop)

- (NSArray *)popViewControllers:(int)numPops animated:(BOOL)animated {
    NSMutableArray* returnedControllers = [NSMutableArray array];
    int indexToPopTo = self.viewControllers.count - numPops - 1;
    for(int i = indexToPopTo+1; i < self.viewControllers.count; i++) {
        UIViewController* controller = [self.viewControllers objectAtIndex:i];
        [returnedControllers addObject:controller];
    }
    UIViewController* controllerToPopTo = [self.viewControllers objectAtIndex:indexToPopTo];
    [self popToViewController:controllerToPopTo animated:YES];
    return returnedControllers;
}

@end

And then from the view controller you can:

NSArray* poppedControllers = [self.navigationController popViewControllers:2 animated:YES];
like image 43
bbrame Avatar answered Jan 06 '23 00:01

bbrame