Reputation: 433
Here is how I remove UIViewControllers which never will be used again in the app.
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
[allViewControllers removeObjectAtIndex:0];
[allViewControllers removeObjectAtIndex:1];
Is this the right way to remove view from the stack ?
While running the app with Instruments I notice that the memory is not freed when the app enter the stage where the code above is executed. What is wrong here ?
Upvotes: 0
Views: 658
Reputation: 2471
By assigning your view controllers into a new mutable array you increased their retain count by 1, removing them decreases their retain count by 1 so the net effect is zero. What you want to do is replace the view controllers that are part of the navigation controller after removing them, there by making the navigation controller release your old instances.
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[allViewControllers removeObjectAtIndex:0];
[allViewControllers removeObjectAtIndex:1];
[self.navigationController setViewControllers:allViewControllers animated:YES];
Upvotes: 0
Reputation: 112873
Creating a new NSMutableArray
of the viewControllers
and then removing from the new NSMutableArray
accomplishes nothing. The viewControllers
still are retain by the navigationController
.
Upvotes: 3
Reputation: 8109
you are not actually freeing up the viewControllers. you are just increasing their retain count by +1 and then decreasing by -1.
actually nothing is getting affected by the code you have written to free your viewControllers.
For a NavigationController, you call popToRootViewController to release all other view controllers except the base/root viewcontroller for NavigationController.
Upvotes: 0