oneonetwo
oneonetwo

Reputation: 97

Best practice for managing multiple UINavigationController stacks

I have an application where I have 3 UINavigation Stacks that can be switched between each other via a custom menu. When switching from Stack A to either Stack B or C, it looks like the new section is being pushed onto the current navigation stack, as the RootViewController for Stacks B/C both have back buttons that pop back to the previous stack. User's can either navigate back to Stack A using the custom menu, or by hitting the back button on Stack B/C RootViewController, which brings them back to where they were in Stack A.

The problem I'm having is figuring out the best way to keep track of whether a user is in Stack A. If they are on the 4th drill down in Stack A, switch to Stack B, and then switch back to Stack A, I need to show exactly where they previously were in Stack A's flow.

Should I be using multiple UINavigationControllers, or is there perhaps a way to achieve this without as much hassle (i.e. possibly using UIViewController Containment)?

Upvotes: 1

Views: 1120

Answers (2)

Artem Stepanenko
Artem Stepanenko

Reputation: 3501

As long as you don't have any problems with performance, my proposition is to keep all required view controller stacks inside separate UINavigationControl-s. So you would have as many navigation controller objects as items in your menu. In this case you would have rather less problems and your logic would be cleaner.

This approach doesn't require much memory, because the most consumable thing is UIView hierarchy and not UIViewController-s or UINavigationController-s.

Upvotes: 0

Fernando Mazzon
Fernando Mazzon

Reputation: 3591

You could use containment to somehow change the navigation controller, yes, but you certainly don't need to do that. You can grab the whole stack within a UINavigationController before replacing it and keep track of your 3 stacks, in a structure like an array or dictionary.

typedef {
   Section1,
   Section2,
   Section3
} Section;

..

@property (nonatomic, assign) Section currentSection;
@property (nonatomic, strong) NSMutableArray currentStacks; //Initialize this was the base stacks for each section (i.e an NSArray with just 1 controller for each slot)
@property (nonatomic, strong) UINavigationController *navigationController;

..

- (void)setSection:(Section)section
{
    self.stacks[self.currentSection] = [self.navigationController.controllers copy];//Save stack for the current section
    [self.navigationController setViewController:self.stacks[section] animated:YES];
    self.currentSection = section;
}

Upvotes: 1

Related Questions