I know how to listen to the physical device changing orientation by observing UIDeviceOrientationDidChangeNotification
. Instead of listening for the device changing, what notification would tell me that the interface has changed? The interface changing is actually a subset of the device changing, because each view controller can choose to support only some orientations.
I am aware that the view controller can implement didRotateFromInterfaceOrientation:
, but I'm looking for a notification rather than a callback function because I need to react to orientation changes in a regular controller, not a view controller. This is a controller for the camera. I would like to put all my orientation handlers in this controller instead of repeating it over and over in all the view controllers that use the camera controller.
I'm not sure what do you mean by "regular controller" but if you would like to be notified on orientation change for each of your UIViewControllers than you could create abstract UIViewController which implements didRotateFromInterfaceOrientation:
method, where you can post your custom notification. Than make each UIViewController a subclass of that abstract UIViewController. Eg.
#import <UIKit/UIKit.h>
@interface MyAbstractViewController : UIViewController
@end
@implementation MyAbstractViewController
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotificationName" object:self];
}
@end
Create your UIViewControllers as subclass of MyAbstractViewController
#import <UIKit/UIKit.h>
#import "MyAbstractViewController.h"
@interface ViewController : MyAbstractViewController
@end
Make required objects as observers of "MyNotificationName"
#import <Foundation/Foundation.h>
@interface MyController : NSObject
@end
@implementation MyController
-(id)init {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vcDidChangeOrientation:) name:@"MyNotificationName" object:nil];
return self;
}
return nil;
}
-(void)vcDidChangeOrientation:(NSNotification *)notification {
UIViewController *vController = (UIViewController *)[notification object];
//Do whatever you want to do with it
}
@end