title: View Controllers: How to switch between views programmatically?
body: <p>In short: I want to have two fullscreen views, where I can switch between view A and view B. I know I could just use an Tab Bar Controller, but I dont want to. I want to see how this is done by hand, for learning what's going on under the hood.</p>
  
  <p>I have an UIViewController that acts as an root controller:</p>
  
  <pre><code>@interface MyRootController : UIViewController {
      IBOutlet UIView *contentView;
  }
  @property(nonatomic, retain) UIView *contentView;
  
  @end
  </code></pre>
  
  <p>The contentView is hooked up to an UIView which I added as an subview to the "view" of the Nib. This has green color and I see it fullscreen. Works fine.</p>
  
  <p>Then, I created two other View Controllers pretty much the same way. ViewControllerA and ViewControllerB. ViewControllerA has a blue background, ViewControllerB has a black background. Just to see which one is active.</p>
  
  <p>So, in the implementation of myRootController, I do this:</p>
  
  <pre><code>// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  - (void)viewDidLoad {
      [super viewDidLoad];
  
      ViewControllerA *vcA = [[ViewControllerA alloc] initWithNib];
      [self.contentView addSubview:vcA.view];
  
      [cvA release];
  }
  </code></pre>
  
  <p>By the way, the -initWithNib method looks like this:</p>
  
  <pre><code>- (id)initWithNib { // Load the view nib
      if (self = [super initWithNibName:@"ViewA" bundle:nil]) {
          // do ivar initialization here, if needed
      }
      return self;
  }
  </code></pre>
  
  <p>That works. I see the view from ViewControllerA when I start the app. But now the big question is: A View Controller typically has all those methods like:</p>
  
  <ul>
  <li>(void)viewWillAppear:(BOOL)animated;</li>
  <li>(void)viewDidDisappear:(BOOL)animated;</li>
  <li>(void)viewDidLoad;</li>
  </ul>
  
  <p>...and so on. Who or what, or how would those methods be called if I do it "my" way without a tab bar controller? I mean: If I allocate that ViewController's class and the view get's visible, would I have to take care about calling those methods? How does it know that viewWillAppear, viewDidDisappear, or viewDidLoad? I believe that the Tab Bar Controller has all this "cleverness" under the hood. Or am I wrong?</p>
  
  <p>UPDATE: I've tested it. If I release the view controller (for example: ViewControllerA), I will get no log message on viewDidDisappear. Only when allocating and initializing the ViewControllerA, I get an viewDidLoad. But that's it. So all signs stand for the cleverness of UITabBarController now ;) and I have to figure out how to replicate that, right?</p>