UIDeviceOrientationDidChangeNotification仅触发一次

| 我正在尝试在其中一个视图的视图控制器上处理设备方向更改。 这是我的代码:
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@\"viewDidLoad\");
    // Tell the UIDevice to send notifications when the orientation changes
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

// tell the director that the orientation has changed
- (void) orientationChanged:(NSNotification *)notification
{
    NSLog(@\"orientationChanged\");
}
当我第一次启动该应用程序时,会调用directionChanged选择器,但是此后无论我旋转iPad多少都不会再次调用它。有人知道我在做什么错吗?当我在应用程序委托中放置类似的代码时,它可以正常工作,但是在此特定的视图控制器中,它的行为不正确。     
已邀请:
我知道这个问题有点老了,但是我以为我会在我处于相同情况时找到的教程中添加一个链接(无论接口的方向如何,都希望获得设备旋转的通知) 。 本教程简要概述了如何使用
UIDeviceOrientationDidChangeNotification
处理设备旋转。 我还遇到了
UIDeviceOrientationDidChangeNotification
不触发的问题(它在模拟器中工作正常,但在我的设备上不工作)。经过一番调试后,我意识到我的设备锁定了“人像方向”功能,这导致“ 1”无法触发。不确定这是否是您的问题,但是这很容易忽略并且值得检查。     
问题是除非您指定可以改变方向,否则方向不会改变。旋转设备/模拟器时,框架会调用
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
其中界面方向是系统要使用的方向,但是您必须返回YES或它不会改变。 如果您未返回YES,则您的方向不会改变,因此您不会收到有关方向改变的通知(因为它没有发生)     

要回复问题请先登录注册