0

I use two ViewControllers. A MainView and a FlipViewController. The MainView contains a bunch of buttons and controls, and a graphics display, and the FlipView contains the settings.

The MainView loads a different XIB depending on orientation to facilitate the variety of buttons I support and the graphics screen. This allows great flexibility to lay things out differently. The project was created initially before the StoryBoarding features were added to Xcode.

The normal usecase is I start in Portrait mode on the MainView, and then touch a button that rotates the FlipView into view. I do things on the FlipView (configure aspects of the program) and then commit the change, which rotates me back to the MainVIew. All is good so far.

The problem happens when I enter the FlipView and then rotate the device so the FlipView takes a different orientation. Lets say I rotate Portrait to Lanscape. Then return from the FlipView to the MainView. At this point, the MainView does not know about the Rotation that FlipView performed. So it is still showing the MainView in Portrait with the Portrait XIB.

The MainView's willRotateToInterfaceOrientation never gets called after returning from the FlipViewController. Thus it never finds out about the orientation change, and consequently, retains the incorrect XIB for that orientation. Here is a sample of the orientation handler in willRotateToInterfaceOrientation...

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration    {

// if portrait, load the portrait XIB for Pad or Phone

if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {

    if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

        [[NSBundle mainBundle] loadNibNamed:@"MainViewiPad" owner:self options:nil];

    }else{

        [[NSBundle mainBundle] loadNibNamed:@"MainView" owner:self options:nil];

    }

    [self viewDidLoad];

    intorient = 1;
 else.... handle Landscape...

So I have been experimenting with the viewWillAppear routine trying to find ways to cause the MainView to find out about the rotation and perform it in a system-wise manner. But so far I do not know how to cause the system to work in this manner. I suspect I will have to generate code that forces a reload of the correct orientation when viewWillAppear gets called. But this is not working correctly, because I never seem to get the change loaded in time, and it seems incorrect to try to reload a new XIB from within the viewWillAppear message.

Any help in understanding this behavior is much appreciated.

Paul3333
  • 1
  • 1

1 Answers1

0

OK. I found the issue here. The problem was that the information about rotation was not getting sent back to the MainView. This is easily handled by having the FlipView (who will be reacting to these orientation messages) resend the message down to the MainView. My code inside the FLipView was not doing anything before for the rotation messages (it was auto handling the rotation), but now I add the handler in the FlipView like this:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
        [mainView willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 
}

Which is essentially passing the message for rotation back to the MainView. This works because the MainView does not go away when the FlipView is opened. It stays in back, and can still respond to the messages it gets.

One thing to notice is that I have a reference to mainView in the code above. You will need to pass this in after the FlipView alloc-init to ensure you can make this call if required. This means adding a new property to the MainView class in the Flipview code.

I do something like this in MainView prior to opening the FlipView:

FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];


controller.delegate = self;
controller.mainView = self;
controller.lastIndexPath = lastIndex;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];

Hope this helps others having this type of trouble. I noticed the same behavior when I opened a web viewer modally as well. I used the same technique there to handle the same type of issue.

Paul3333
  • 1
  • 1