views:

1334

answers:

3

Hey,

every UIViewController has a method called willRotateToInterface.

Is it possible to do this within a UIView too?

Does this match the idea of model view controller ?

The only way I can think of is to send the event from the UIViewController to the UIView.

Is there a global variable for the current orientation?

+6  A: 

Observe UIDeviceOrientationDidChangeNotification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

...

- (void)orientationDidChange:(NSNotification *)note
{
    NSLog(@"new orientation = %d", [[UIDevice currentDevice] orientation]);
}

UIDevice Class Reference

I should note that yo uneed to add -beginGeneratingDeviceOrientationNotifications when you want these notifications to be sent, and call -endGeneratingDeviceOrientationNotifications when you want them to stop. There is a battery impact of generating these, so you should only do so when your view is on screen. UIViewController does all this for you, so if you have a view controller, it is worth letting it do the work.

Rob Napier
+2  A: 

You can subscribe to a global notification and get a call when the device is rotated, it wont do anything for you though..

[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
Daniel
A: 

If you just want to adjust view to new size/layout when orientation changes, you should just override its layoutSubviews method.

It will be called whenever size of the view changes, which usually happens when view is rotated.

porneL