views:

22

answers:

1

Hello all, I just tried adding some print statements to my shouldautorotate method and noticed that it checks it 4 times which does make sense but even though I am not switching mode from portrait to landscape,

it returns portrait 3 times and on the fourth time, it returns landscape even though my simulator is not in landscape.

if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
   NSLog(@"landscape left");
  }else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
   NSLog(@"landscape right");
  }else if(interfaceOrientation == UIInterfaceOrientationPortrait){
   NSLog(@" portrait");
  }else if(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
   NSLog(@"portrait upsidedown");
  }

Any one knows why?

+1  A: 

Try putting that code into the didAutorotate or the willAutorotate method. shouldAutorotate is only supposed to return YES or NO.

I'm theorising that shouldAutorotate is checked regularly, whereas didAutorotate is only fired of when it detects an orientation shift.

This is the code I use to check:

- (void) reOrient{
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){  

    } else {

    }
}

That is in a method I created called reOrient which is called from my didAutorotate

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    [self reOrient];
}

Just make sure when you create a new method like reOrient that you declare it in the header as well (I kept forgetting when I was starting out) as below:

- (void)reOrient;
Daniel Hanly
Thanks for helping out. I am trying to find that method in the documentation.
if your using X-code, start typing - (void)didAutorotate... and the rest of it'll appear. You don't need to do anything as this is a built in method and so fires automatically. The reOrient method was a custom one I created, specific to my application and features the above if statement.
Daniel Hanly
Thanks managed to find it in the uiviewcontroller.
If it works for you, could you please accept the answer? I've added a little more information that may help you
Daniel Hanly
Ofc ! thanks you are a star.