0

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

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

4

1 回答 1

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

将所需对象作为“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
于 2014-07-07T22:19:38.697 回答