Question

I'm working on an app that uses NavigationController based Storyboard, but the navigation is controlled by a segmentedControl. This way I can iterate over the x menus several times, then I can go back for about a year :D

My question is: How can I override the push methods of the NavigationController to check if there's already an instance in the stack and reuse that without adding the same instance to the stack again? Thank you in advance!

Was it helpful?

Solution

Subclass the UINavigationController and implement the pushViewController:animated: method in the subclass. There you can iterate through the stack by calling self.viewControllers which returns an NSArray of the view controllers on the stack. There you can check, whether the controller you're trying to push has already been pushed before. If not, call [super pushView...]. If it has, create a mutable copy of the stack NSMutableArray * newStack = [self.viewControllers mutableCopy] and move the desired view controller to the end of the array. Once you have that, just call [super setViewControllers:newStack animated:...] which sets your new stack manually.

I don't think you need to override the popViewController method, though.

Oh, and once you have that, don't forget to change the class of the UINavigationController in the Storyboard to the name of your subclass of the navigation controller.

Another thought - if you're using the segmented control, have you thought about using UITabBarController instead of UINavigationController for navigation?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top