I am trying to make an app where each view controller (.h/.m) has 2 NIB files... one for portrait, one for landscape. Is this the "standard" way of supporting orientation or must I manually set up the orientation view programmatically? The problem I am facing is that when a user flips the orientation, all views are reset (so the user must re-enter text fields/views input).
Here is my orientation method:
- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
if(portrait) {
[[NSBundle mainBundle] loadNibNamed:@"myview-portrait" owner:self options:nil];
[self setupLayout];
} else{
[[NSBundle mainBundle] loadNibNamed:@"myview-landscape" owner:self options:nil];
[self setupLayout];
}
[UIView commitAnimations];
}
Apple has a number of different suggestions for supporting multiple orientations in their View Controller Programming Guide in the section titled "Managing a View Controller’s Interface Orientation". You might want to read that section to see if any of their suggestions would better suit your needs.
That being said, I have used the strategy you have presented above in an application and it seemed to work pretty well.
To solve your problem of the views being 'reset' I would suggest that you keep a reference to the data being entered by the user as they move from control to control. Then when your orientation changes, you can repopulate the controls so the user's 'progress' isn't lost.
来源:https://stackoverflow.com/questions/4346597/iphone-orientation-and-2-nib-files