1

My iPhone app is based on a common "utility template" like Apple's own Weather app.

I click on the info-button and it flips the screen around. I click on my done button... and it flips back. That part all seems to work ok.

I've placed NSLog() statements into each of 4 methods in my FlipSideViewController.m

viewDidLoad
viewWillAppear
viewDidUnload
viewWillDisappear

Shouldn't I see viewDidLoad and viewDidAppear being called when I flip TO my FlipSide. And then see viewWillDisappear and viewDidUnload when I flip back?

Instead, I never see any viewDidUnload call. But I DO see another viewDidLoad each time I flip TO my FlipSide. It's that wrong?

Flipping back and forth, again and again, I would see:

viewDidLoad
viewWillAppear
viewWillDisappear

viewDidLoad
viewWillAppear
viewWillDisappear

viewDidLoad
viewWillAppear
viewWillDisappear

Doesn't that mean the view reloaded 3 times... but unloaded 0 times? Shouldn't there be "matching" load/unload and appear/disappear methods happening here?

Patricia
  • 289
  • 3
  • 6
  • 1
    could you post the code for your flip effect ? i think you're not removing the view as you should. – vincent Nov 29 '10 at 18:13

1 Answers1

7

I thought so too at first, but apparently that's not the case.

The viewDidUnload method actually only gets called when the view controller gets a memory warning.

Nonetheless, the view will be released when the view controller is dealloc'd.

So, if you are releasing stuff like IB outlets in viewDidUnload, that's good, but you have to release them in dealloc as well, because viewDidUnload won't be called under normal circumstances (i.e., if you don't get memory warnings).

EDIT: to release the view you just have to call dealloc on the super class UIViewController in your dealloc:

- (void) dealloc
{
    // release your stuff, anything that you alloc or retain in your class

    // then call `dealloc` on the super class:
    [super dealloc];
}

in the viewDidUnload method you only have to release things that should get unloaded along with your view, usually things that you connected to IBOutlets in interface builder.

- (void) viewDidUnload
{
    // if the property was declared with the "retain" keyword, you can
    // release it simply by setting it to nil like this:
    self.myOutlet = nil;
}
filipe
  • 3,370
  • 1
  • 16
  • 22
  • It's worthwhile to note that reusing the *same* view controller (i.e., not releasing it) will result in calls to `viewDidUnload` more often, as the view controller itself will still be around, but the view may be off-screen (and the application can therefore unload it). – Justin Spahr-Summers Nov 29 '10 at 19:00
  • So what should my code look like inside viewDidUnload... and then again in dealloc... to release correctly? (Also, won't I be releasing something that is already released?) – Patricia Nov 29 '10 at 19:12