2

Anyone know how to do this?

I thought this:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

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

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
}

- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}

- (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
}

- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {
}

may prevent the device from rotation (overriding all rotate methods of my UIViewController and not calling the superclass) but I fear it's not the UIViewController that actually performs the rotation.

My UIViewController is in a UINavigationController.

Anyone have any ideas?

Cheers, Nick.

Quinn Taylor
  • 44,553
  • 16
  • 113
  • 131
Nick Cartwright
  • 8,334
  • 15
  • 45
  • 56
  • 1
    You actually shouldn't need to override all those empty delegate methods, only the ones you actually need to use. The caller should test if your subclass responds to a particular method before calling it, regardless of whether you define it. It's just the way delegates work. – Quinn Taylor Jun 15 '09 at 17:01

3 Answers3

20

You can register for the notification UIDeviceOrientationDidChangeNotification (from UIDevice.h), and then when you care about orientation changes call this method:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

When you no longer care about orientation changes, call this method:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

The notification will be sent when applicable, and you can check [UIDevice currentDevice].orientation to find out what the current orientation is.

Community
  • 1
  • 1
Ecton
  • 10,702
  • 2
  • 35
  • 44
4

On a side note shouldAutoRotateToInterfaceOrientation used to be called on the time for rotation in 2.x, but in 3.0 it's much less consistent. The notification mentioned in the other post is the way to go for reliable indication of rotation.

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
1

You're returning YES from shouldAutorotateToInterfaceOrientation:, which I suspect you didn't mean to do. That's the only method you need to implement to prevent the device from rotating.

As for getting the current orientation of the device, you can use [[UIDevice currentDevice] orientation].

Nathan de Vries
  • 15,481
  • 4
  • 49
  • 55
  • Hey there - thanks for your suggestion! [[UIDevice currentDevice] orientation] will get the current orientation - but I need to detect when this happens without the phone auto-rotating. If I return NO from shouldAutorotateToInterfaceOrientation, none of the delegate functions get called so I loose the ability to detect when rotation happens. Does anyone else have any ideas? Cheers, Nick. – Nick Cartwright Jun 15 '09 at 15:54