Samui
Samui

Reputation: 1384

NavigationController Stack

First of all, sorry for my bad english. I'll try to explain my question:

I have a RootViewController (Navigation based project). So, it shows the tableview and when the user select a row of the table (didSelectRowAtIndexPath) y do the following to show the next view:

NextViewController *nextView = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
[self.navigationController pushViewController:nextView animated:YES];
[nextView release];

What happens if the user select the back button of the navigationbar and select again the row, and do this repeatedly? A lot of new views (instances of NextViewController) is being created (a lot of memory allocating)? Or is he just navigating the stack?

Can you help me? I dont want to waste memory in that way (if is the case). Thanks!

Upvotes: 1

Views: 1461

Answers (1)

Tim
Tim

Reputation: 60110

If the user toggles back and forth from your RootViewController to a NextViewController repeatedly, here's what happens:

  1. The NextViewController is created (alloc'd) in your didSelectRowAtIndexPath: method. Because you called an init method on it, you're responsible for releasing it.
  2. You push nextView onto the navigation controller stack, which retains it.
  3. You release nextView, so the only thing with a retain on it is the navigation controller.
  4. Once your user moves back from the NextViewController, the navigation controller releases it. Now nothing is retaining nextView, so it gets dealloc'd. The memory is freed.

Basically, you do create a NextViewController every time your user moves back and forth (you're not "just navigating the stack," since objects are changing each time), but you're not leaking a large amount of memory or holding on to each controller you create. Your memory usage here is fine.

Upvotes: 1

Related Questions