Reputation: 470
While we push pages on the nav stack, I was wondering If I could restrict the maximum number of pages to 10 or so to r. For Example,
If the limit is 4 : A -> B -> C -> D now D is on top and A is the root. When I try to push E, it should remove B from the stack. Since I need rootVC to remain in place. result should be : A -> C -> D -> E.
Also, I know how to remove and add View controllers to the stack, my problem is how to keep track of the navStack while the app is running.
Upvotes: 1
Views: 491
Reputation: 11343
Try this code:
- (void)limitNavigationStack{
NSMutableArray* controllers= [self.navCtrl.viewControllers mutableCopy];
if(controllers.count> MAX_CONTROLLERS){
[controllers removeObjectAtIndex:1]; // Remove first object after root
[self.navCtrl setViewControllers:controllers];
}
}
You can put this code in your appDelegate and call it from viewDidLoad
of your viewController.
To make it more generic as you have pointed out, you can inherit from a base view controller and call this method from its viewDidLoad
Upvotes: 1
Reputation: 2417
UINavigationController
has a property named viewControllers
. Now just before you are going to push a new view controller you can always check how many objects are present in self.navigationController.viewControllers
.
If it exceeds your limit then you can go for the navigation stack management manually.
if(self.navigationController.viewControllers.count > limitCount) {
// Do the stack management manually for nav controller.
}
Upvotes: 0