0
votes

I have the following controllers and views in my project:

PortraitController.m
PortraitView.xib
LandscapeController.m
LandscapeView.xib

Portrait controller is used as a controller for the portrait view, and a landscape controller is used as a controller for the landscape view.

I need to switch BOTH controller and view when device orientation changes. This approach is advised by Apple, but they do not provide an usable, general example of this!

I have found various examples, but they are not quite what I need - one controller + two views in same xib, push+pop from navigation controller, place second view over previous, ... none of them is what I need.

What is the simplest way to switch (with animation) between these two Controller+View combinations when device orientation changes?

Is there a solution which can work for both iOS 5 and iOS 6?

2
Where does apple advice changing the controller and view on orientation changes? - Goz
developer.apple.com/library/ios/#featuredarticles/… Section: Creating an Alternate Landscape Interface - Dusan
Do you need to share information among the two? - perrohunter
@Dusan: Thanks! Thats very interesting! - Goz
Basically not, I know how to manage that in other way so no need to share directly when switching - Dusan

2 Answers

3
votes

The example that Apple provides uses the Storyboard's segue mechanism to go from one view to another, if you are using NIB files you'd need to go with the UINavigationViewController approach, you can even set the transition you want to go from one UIControllerView to another

For example, this code would switch from one UIViewController to another with a page turning animation

[UIView  beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.75];
[self.navigationController pushViewController:yourViewController animated:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:navController.view cache:NO];
[UIView commitAnimations];

To achieve the fact of knowing when to push one view or another (or use a segue if you switch to storyboards) you could register to listen to the UIDeviceOrientationDidChangeNotification event from your navigation controller or the current view, to register a certain method to handle screen rotation for example

-(void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(methodToChangeViewController) name:UIDeviceOrientationDidChangeNotification object:nil];

}

-(void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

It's very important to unregister the event

Hope this can give you some insight on how to achieve what your are intending

2
votes

Here is a good approach for your problem: Rotating to Show Different iOS Views. With NSNotificationCenter you can manage the rotation event and push/pop a controller, or change any view you want. Hope this can help you.