views:

111

answers:

1

Hi all, I cant quite see why Im having this issue but I am..... Basically I have a Tabr bar controller with a navigationController in one of the tabs. In this particular tab I want the user to be able to rotate the device and see a completely different view, not the original view rotated! In order to achieve this (at full screen) I have a LandscapeViewController which I present modally when the device is in landscape orientation. The problem occurs when I present the modalViewController.....in the LandscapeViewController I also check the orientation so I can dismiss self when the user goes back into portrait mode. However when I present it I get a callback saying its going into portrait mode, which completely messes it up! Here's the code and log statements to clarify whats goin on..

//in PortraitViewContoller

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

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
  NSLog(@"Norm going Portrait");

  [self changeTheViewToPortrait:YES andDuration:duration];

    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
  NSLog(@"Norm going Landscape");
  [self changeTheViewToPortrait:NO andDuration:duration];
    }
}


(void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:5.];

    if(portrait){ 
  NSLog(@"dismissed");
  [self dismissModalViewControllerAnimated:NO];
    }
    else{ 
  NSLog(@"presented");
  LandscapeOverviewViewController *land = [[LandscapeOverviewViewController alloc]initWithNibName:@"LandscapeOverviewViewController" bundle:nil];
  land.delegate = self;
  [self presentModalViewController:land animated:NO]; 
  [land release];
    }

    [UIView commitAnimations];
}

//in LandscapeViewController

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

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
  NSLog(@"Modal is going Portrait");
  [self changeTheViewToPortrait:YES andDuration:duration];

    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
  NSLog(@"Modal is going Landscape");

  [self changeTheViewToPortrait:NO andDuration:duration];
    }
}

 (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];

    if(portrait){ 
  NSLog(@"calling delegate to dismiss");
  [delegate landscapeViewController:self didRotateBack:(YES)]; 
    }

    [UIView commitAnimations];
}

and this is the log text

Norm going Landscape

2010-08-24 15:29:40.024 App[32461:207] presented

2010-08-24 15:29:40.026 App[32461:207] Modal is in going Portrait

2010-08-24 15:29:40.026 App[32461:207] calling delegate to dismiss

2010-08-24 15:29:40.027 App[32461:207] delegate called

As you can see when in portrait and I rotate it does the correct thing by presenting the landscapevc, but then it thinks its in portrait and tries to dismiss?

Can anyone see where Im going wrong, and apoligies for the length of this but the formatting of code wouldnt work properly otherwise.

Many thanks

Jules

+1  A: 

Have you implemented the shouldAutorotateToInterfaceOrientation: method in the landscape view controller? If not, this view controller will be forced to stay in portrait mode, which will in turn trigger your dismissal code.

It's also worth noting that sometimes view controllers are added to a window in a certain orientation, and then rotated. It's worth it to check the orientation that willRotate passes to you against your current rotation. I haven't verified this in quite some time, but in the late 2.x / early 3.0 days, this was common.

Jerry Jones
Thanks for the reply, yes I had implemented shouldAutorotateToInterfaceOrientation:. In the end I scrapped using the autorotate and just did this in the relelvant viewController.[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];and rotated when neccessary. Many thanksJules
Jules