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

MyAbstractViewController のサブクラスとして UIViewControllers を作成します。

 #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 に答える