title: iPhone dev - viewDidUnload subviews
body: <p>I'm having a hard time undestand a couple of the methods in UIViewController, but first I'll say what I think they are meant for (ignoring interface builder because I'm not using it):  </p>
  
  <p><code>-init</code>: initialize non view-related stuff that won't need to be released in low memory situations (i.e. not objects or objects that can't be recreated easily).<br>
  <code>-loadView</code>: create the view set the <code>[self view]</code> property.<br>
  <code>-viewDidLoad</code>: Create all the other view elements<br>
  <code>-viewDidUnload</code>: Release objects created in <code>-viewDidLoad</code>.<br>
  <code>didReceiveMemoryWarning</code>: Low-memory situation, release unnecessary things such as cached data, if this view doesn't have a superview then the <code>[super didReceiveMemoryWarning]</code> will go on to release (unload) the view and call <code>-viewDidUnload</code>.<br>
  <code>-dealloc</code>: release everything<br>
  <code>-viewWillAppear:</code>, <code>-viewDidAppear:</code>, <code>-viewWillDisappear:</code>, <code>-viewDidDisappear:</code> self-explanatory, not necessary unless you want to respond (do something) to those events. </p>
  
  <p>I'm not sure about a couple of things. First, the Apple docs say that when <code>-viewDidUnload</code> is called, the view has already been released and set to nil.  </p>
  
  <ol>
  <li>Will <code>-loadView</code> get called again to recreate the view later on?</li>
  <li>There's a few things I created in <code>-viewDidLoad</code> that I didn't make a ivar/property for because there is no need and it will be retained by the view (because they are subviews of it). So when the view is released, it will release those too, right? When the view is released, will it release all its subviews? Because all the objects I created in <code>-viewDidLoad</code> are subviews of <code>[self view]</code>. So if they already get released why release them again in <code>-viewDidUnload</code>? I can understand data that is necessary when the view is visible being loaded and unloaded in these methods, but like I asked, why release the subviews if they already get released?</li>
  </ol>
  
  <p><strong>EDIT</strong>: After reading other questions, I think I might have got it (my 2nd question). In the situation where I just use a local variable, alloc it, make it a subview and release, it will have a retain count of 1 (from adding it as a subview), so when the view is released it is too. Now for the view elements with ivars pointing to them, I wasn't using properties because no outside class would need to access them. But now I think that that's wrong, because in this situation:</p>
  
  <pre><code>// MyViewController.h
  @interface MyViewController : UIViewController {
      UILabel *myLabel;
  }
  
  // MyViewController.m
  . . .
  - (void)viewDidLoad {
      myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 10)];
      [myLabel setText:@"Foobar"];
      [[self view] addSubview:myLabel];
  }
  
  - (void)viewDidUnload [
      // equivalent of [self setMyLabel:nil]; without properties
      [myLabel release];
      myLabel = nil;
  }
  </code></pre>
  
  <p>In that situation, the label will be sent the <code>-release</code> message after it was deallocated because the ivar didn't retain it (because it wasn't a property). But with a property the retain count would be two: the view retaining it and the property. So then in <code>-viewDidUnload</code> it will get deallocated. So its best to just always use properties for these things, am I right? Or not?  </p>