Orientation problems of the iPad when switching between two views and from portrait to landscape, vice versa

advertisements

I am building a simple app with two View Controllers, I am testing the code using the iPhone Simulator, everything seem to be working fine. The problem happens when I rotate from Portrait to Landscape or from Landscape to portrait. This is the logic of the app, the app always launched in Portrait, I have a button to on the first View to Switch from View1 to View2. On View2 I have another button to switch from View2 back to View1. Let say, I am in Portrait mode, I switch from View1 to View2, then rotate the iPad (in the simulator) from Portrait to Landscape, when I switch back from View2, i.e to go back to View1. View1 screen/view is displayed in Portrait with View2 screen displayed in the background, ie part of View2 is displayed in the background, I guess because View1 was originally in Portrait mode.

The question is.. Has anyone had this issue before, if so, any code to fix this issue, secondly, how can I identify in the code which orientation the device is and which orientation the view is in.

This method is to switch to View 2:

-(IBAction) switchToView2: (id) sender {

SecondViewController *myViewController = [[SecondViewController alloc]        initWithNibName:@"SecondViewController" bundle:nil];

[self.view addSubview: myViewController.view];
[UIView commitAnimations];
}

This method is to switch back to View1:

-(IBAction) switchBackToView1:(id) sender {

[self.view removeFromSuperview];
[UIView commitAnimations];

}


From your code:

[self.view addSubview: myViewController.view];

It makes me believe that your 2 views is a subview of myViewController.view. which explains why they're both showing at the same time. it would make sense to have seperate view controllers for different views.