Question

Supposedly I have a UIViewController A and a UIViewController B. From A, I call the method presentViewController:B. When B shows up, what happens to A? Is it removed from the memory? If not, what method should I call to delete it?

If my UI flow is like this, A->B->A->B->A->B->... and so on, how to prevent the memory from increasing accordingly?

Was it helpful?

Solution

When you use the presentViewController:animated:completion: method from controller A to present controller B modally, what happens is that the presentedViewController property of A is set to controller B, and the presentingViewController property of B is set to A. Thus, both controllers are kept in memory while the presentation is taking place.

When you go from B to A, you call dismissViewControllerAnimated:completion: on A via the presentingViewController property of B, like this:

[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

(You can also call [self dismissViewControllerAnimated:YES completion:nil] and the system will automatically forward the request to the presenting view controller.)

After that, the presentedViewController property of A will be set to nil and, consequently, it will be subject to memory deallocation by the system, provided that there isn't any other strong pointer pointing to it.

OTHER TIPS

No, A won't be removed from memory. And if you want to alternate between A and B you can either:

  • use a UINavigationController and push B, then pop it off again and you're back at A
  • or make B the new root controller of your window and then A again and add a proper transition
  • or use a as the root and present B. Then dismiss B and you're back at A.

The UIViewController will stay in memory unless you popped it.

Use push & pop instead of just pushing new UIViewControllers.

Instead of this A->B->A->B->A->B->

You'll get this A<->B

Have a root view controller when ever A completes it's task let your root view is responsible of removing A and and presenting B. Like wise when B finishes its task alert your root view to remove B and show A again.

It won't be released automatically. You can release it manually by using dismissModalViewControllerAnimated.

Here is a very good tutorial to see this : Link

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