title: How to switch views programmatically
body: <p>I've found many tutorials on switching views on the iPhone but none that quite match my needs. I wonder if you can help.</p>
  
  <p>I'm looking to have an initial view, A and on a button press unload A and switch to view B.</p>
  
  <p>I've seen the following, but I have reasons why I don't want to do it that way:</p>
  
  <p>Using a navigation controller - Once view A is discarded, I don't need to return to it, so it should disappear completely and be deallocated from memory.</p>
  
  <p>Using a Tab bar controller - same reason as above. I'd like to linearly move from A to B and not B to A - ever.</p>
  
  <p>Using a modal view - actually, the same reason. Also this feels like a bit of a UI no-no, since it gives the user the impression that they're coming back to view A once an action in B is complete.</p>
  
  <p>Please help me, the one thing I'm really struggling with is how to unload the original view controller that's created as part of the app. I think to show the other view all I need to do is init and alloc it in View A, then add it as a subview. However, unloading view A from viewcontroller A is really baffling me.</p>
  
  <p>Edit: On advice from someone below I've implemented view switching, but for some reason removeFromSuperview doesn't work on View A after View B is displayed. Basically I can still see view A behind view B (I made view b slightly smaller so I could test this).</p>
  
  <p>Here's my app delegate: </p>
  
  <pre><code>@implementation View_SwitchAppDelegate
  
  @synthesize window;
  @synthesize switchViewController;
  @synthesize secondViewController;
  @synthesize firstViewController;
  
  - (void)applicationDidFinishLaunching:(UIApplication *)application {    
  
      // Override point for customization after application launch
      FirstViewController *aFirstView = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
      [self setFirstViewController:aFirstView];
      [aFirstView release];
      [window addSubview:firstViewController.view];
      [window makeKeyAndVisible];
  }
  
  - (void)dealloc {
      [window release];
      [switchViewController release];
      [super dealloc];
  }
  
  - (void)switchToTwo {
      SecondViewController *aSecondView = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
      [self setSecondViewController:aSecondView];
      [aSecondView release];
  
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationDuration:0.5];
      [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
      [firstViewController.view removeFromSuperview];
  
      [firstViewController release];
      firstViewController = nil;
  
      [self.window addSubview:[secondViewController view]];
  
      [window makeKeyAndVisible];
      [UIView commitAnimations];
  }
  
  @end
  </code></pre>
  
  <p>Here's my FirstViewController (You'll see that when the camera picker returns an image, I try to switch views by calling the app delegate method:</p>
  
  <pre><code>#import "FirstViewController.h"
  #import "View_SwitchAppDelegate.h"
  #import "SecondViewController.h"
  
  @implementation FirstViewController
  
  - (IBAction) takeButtonPressed 
  { 
     NSString *type = @"Camera";
      [self callCameraWithType:type];
  } 
  - (IBAction) chooseButtonPressed 
  { 
      NSString *type = @"Library"; 
      [self callCameraWithType:type];
  } 
  
  -(void) callCameraWithType:(NSString *)type {
  
      // Set up the image picker controller and add it to the view
      imagePickerController = [[UIImagePickerController alloc] init];
      imagePickerController.delegate = self;
  
      if ([type isEqual:@"Camera"])
      {
          NSLog(@"Camera");
          if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
              imagePickerController.sourceType = 
              UIImagePickerControllerSourceTypeCamera;
          }
          else {
              imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
          }
      }
      else
      {
          NSLog(@"Library");
          imagePickerController.sourceType = 
          UIImagePickerControllerSourceTypePhotoLibrary;
      }
  
      [self presentModalViewController:imagePickerController animated:YES];
  }
  
  - (void)imagePickerController:(UIImagePickerController *)picker
  didFinishPickingMediaWithInfo:(NSDictionary *)info {
      [picker dismissModalViewControllerAnimated:YES];
  
      //get app delegate
      View_SwitchAppDelegate *appDelegate = (View_SwitchAppDelegate *)[[UIApplication sharedApplication] delegate];
      [appDelegate switchToTwo];
  }
  
  - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  {
      // Dismiss the image selection and close the program
      [picker dismissModalViewControllerAnimated:YES];
  }
  
  // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  - (void)viewDidLoad {
      NSLog(@"View loaded");
      [super viewDidLoad];
  }
  
  - (void)removeSelf{
      [self.view removeFromSuperview];
  }
  
  - (void)viewDidUnload {
      // Release any retained subviews of the main view.
      // e.g. self.myOutlet = nil;
      NSLog(@"First Unload");
  }
  
  - (void)dealloc {
      NSLog(@"First Dealloc");
      [super dealloc];
  }
  
  @end
  </code></pre>