views:

313

answers:

1

How can I setup a view controller to refresh the fields in the view only it is pushed on a navigation controller and releases the objects when it is popped from navigation controller views.

My goal is that for a view controller:

  1. Refresh display only when it is pushed on the navigation controller stack
  2. releases data when it is popped from the navigation controller stack
  3. reuse the controller as much as possible (to eliminate xib files loading/unloading overhead)

Are there any delegate methods for viewWillBePushed: and viewWillBePopped:?

Example

Let's say I have three layers of views:

Movies (MovieListController)
   --> Movie Basic Info (MovieInfoController)
        --> Movie Credits (MovieCreditsController)

Assume we have a class Movie to represent each movie.

Here, I want to only have three UIViewControllers, one instance of each in the runtime of the program. So I can reuse the same MovieInfoController for different movies and similarly MovieCreditsController.

However, I would also like MovieInfoController to only update its view fields (e.g. title, release date, etc) only when it is pushed on the stack. Implementing the display in viewWillAppear will force it to update fields even when MovieCreditsController is popped.

Also, I want MovieInfoController to release its reference to Movie, when it is popped, and any potentially large object (e.g. cover image). Implementing this logic in viewWillDisappear will cause it to release the data when it is pushing MovieCreditsController on the stack.

Is there reasonable? Am I going about it the wrong way?

+1  A: 

You're 90% there because you created a Movie object. MovieListController should own a single MovieInfoController. It should call -setMovie: prior to pushing it onto the navigation controller. The call to -setMovie: should be what updates your fields.

Similarly, MovieInfoController should own a single MovieCreditsController, and should call -setMovie: on it before pushing it onto the nav controller.

In MovieListController and MovieInfoController, you can call [subController setMovie:nil] in their -viewDidAppear: to free up memory.

Rob Napier
That's actually to what I'm doing now, except that it calls `drainFieldsInfo` instead of `setMovie:nil`. I was hoping for something better. Thanks though!
notnoop