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 ?
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.