接口方向更改的UIDeviceOrientationDidChangeNotification的类似常量是什么?

时间:2014-07-07 19:10:11

标签: ios cocoa-touch orientation interface-orientation

我知道如何通过观察UIDeviceOrientationDidChangeNotification来听取物理设备改变方向。而不是监听设备更改,通知会告诉我接口已更改?接口更改实际上是设备更改的子集,因为每个视图控制器可以选择仅支持某些方向。

我知道视图控制器可以实现didRotateFromInterfaceOrientation:,但我正在寻找通知而不是回调函数,因为我需要对常规控制器中的方向更改做出反应,而不是视图控制器。这是相机的控制器。我想将所有的定向处理程序放在这个控制器中,而不是在使用相机控制器的所有视图控制器中反复重复它。

1 个答案:

答案 0 :(得分:0)

我不确定"常规控制器"是什么意思?但是如果您希望收到有关每个UIViewControllers的方向更改的通知,那么您可以创建实现didRotateFromInterfaceOrientation:方法的抽象UIViewController,您可以在其中发布自定义通知。比使每个UIViewController成为该抽象UIViewController的子类。例如

 #import <UIKit/UIKit.h>

 @interface MyAbstractViewController : UIViewController

 @end

 @implementation MyAbstractViewController

 -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
     [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotificationName" object:self];
 }

 @end

创建UIViewControllers作为MyAbstractViewController的子类

 #import <UIKit/UIKit.h>
 #import "MyAbstractViewController.h"

 @interface ViewController : MyAbstractViewController

 @end

将所需对象作为&#34; MyNotificationName&#34;

的观察者
 #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
相关问题