Reputation: 5407
my app involves many viewcontrollers, some of which are memory heave-duty ones.
The user flow is like starting from a home viewcontroller, user will clicks through a sequence of viewcontrollers (i use presentModalViewControler for the transitions). Sometimes, user will click a menu bar or something to go back to a certain viewcontroller he/she went through in the past, or jump forward to a viewcontroller which hasn't been presented yet. So, it's like: A->B->C->D->B->D->E->F->C->I...
What I am doing right now is: no matter where the user is, when he/she needs to jump over to some other viewcontroller, I simply instantiate the target viewcontroller, and present it as a modal viewcontroller.
Well, it works well, but with Instruments, I've identified the allocated memory heap keeps increasing since app launches. My guess is that the parentViewController(or presentingViewController in iOS 5) never get released because it's in the root or middle of the modal presenting stack, so it's like keep adding viewcontroller instances.
Therefore, I realize I'm doing it wrong. I would appreciate if anyone can point me to a right direction of doing apps like this.
Upvotes: 1
Views: 155
Reputation: 385910
When you present a view controller modally, the parent view controller stays around. That's why your memory usage grows every time the user navigates to another view controller.
Try using a UINavigationController
as your window's root view controller. Use your A
view controller as the nav controller's root view controller. Then, when you want to go to your B
view controller, do it like this (in some method of your current view controller):
UIViewController *b = [[BViewController alloc] init...];
[self.navigationController setViewControllers:[NSArray arrayWithObject:b] animated:YES];
As long as you don't keep any other references to your A or B view controllers, they should be deallocated when they're no longer visible.
Upvotes: 2