views:

59

answers:

1

Hello everyone,

I wanted to create a very simple method that switches between views in a view based application. For some reason, when the views are switched, the first view is removed and instead of viewing the second view, I see a white screen.

This is my method:

 FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
 self.view = firstViewController.view; 
 [firstViewController.view removeFromSuperview];
 [firstViewController release];
 secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
 self.view = secondViewController.view;

I don't know why it is happening because I know that the second view's ViewDidLoad method is called (I put a NSLog there) - but the second view is not seen!

Any suggestions?

Thanks in advance,

Sagiftw

+1  A: 

viewDidLoad's executing because initWithNibName:bundle: calls it. That doesn't mean that the view's actually being displayed.

I usually use this (removing initialisation/release logic):

[self.view addSubview: firstViewController.view];
[firstViewController.view removeFromSuperview];
[self.view addSubview: secondViewController.view];
Frank Shearar
Thanks! It is working!!!