views:

886

answers:

2
  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

My code get this event more than once while device is rotating. I'm searching in apple docs for reference but seems no lucky. Could any one offer a hint why this is happening?

My code is an apple doc sample code, without any change but some logs. http://developer.apple.com/iphone/library/samplecode/AlternateViews/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008755

+2  A: 

I'm new at iPhone dev, but I think the reason it's being called numerous times is that shouldAutorotateToInterfaceOrientation isn't an event. It's simply meant to take a UIInterfaceOrientation and return YES or NO depending on whether or not autorotation is supported.

The Apple docs show that UIInterfaceOrientation is defined as:

typedef enum {
   UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
   UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
   UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
   UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;

So all you're supposed to override that method with is something that checks interfaceOrientation for one or more of the above, and says whether or not the view should autorotate to it.

avpx
Thanks for your help.Since my code is downloaded from apple sample, the orientation check has already been there. I just wonder why this method get called with same orientation more than once? I don't quite get this: "it isn't an event". Could you tell me about this? Appreciated.
@lovecactus An event is an action, like a button press. It is meant to spark a series of other UI actions. `shouldAutorotateToInterfaceOrientation` is not meant to be a series of actions, but is merely meant to return whether or not the view should auto-rotate. In fact, the method should not have side-effects. Do you see the contrast?
avpx
+2  A: 

If you return NO for an orientation, it will keep trying until you return YES. For example, if you return NO for landscape right, it may try landscape left. If you return NO for everything, then it will not rotate. And this method may be called at arbitrary times, not strictly when rotation occurs. For example, when a new view is popped onto a navigation controller, the new view will be queried even though no rotation happened.

drawnonward
Thanks for your help. However I logged the orientation in this method. When a rotation is triggered, this method would be called with same orientation. This seems a little bit different from your answer, or I misunderstood it?
"this method may be called at arbitrary times" covers pretty much anything. The system does not document that it will save the response, so it could be called more than once on a single view controller in a single rotation.
drawnonward