2011-04-17 134 views
1

我正在尝试为我的某个视图处理viewcontroller上的设备方向更改。 这里是我的代码: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"); 
} 

当我第一次启动应用程序时,orientationChanged选择被调用,但在此之后,它不会再次呼吁无论多少我旋转的iPad。有谁知道我可能做错了什么?当我在应用程序委托中放置类似的代码时,它可以正常工作,但在这个特定的视图控制器中,它并不适合。

回答

-1

问题是方向不会改变,除非您指定它可以这样做。当你旋转设备/模拟器,框架调用

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;

其中interface定位是系统要使用的方向,但是你必须返回YES或者它不会改变。

如果你没有返回YES那么你的方向没有改变,因此,你不能得到通知的取向改变(因为它没有发生)

+4

shouldAutorotateToInterfaceOrientation用于自动旋转视图。我不希望视图自动旋转。我只想接收设备旋转时间的通知。 – MrPrezident 2011-04-17 19:15:42

0

我知道这个问题是有点老了,但只是以为我会添加一个链接到我在与你处于相同情况时发现的教程(希望获得设备旋转的通知,而不管接口方向是什么)。

This tutorial给出了使用UIDeviceOrientationDidChangeNotification如何处理设备旋转的一个很好和简单的概述。

我也有问题UIDeviceOrientationDidChangeNotification没有射击(它在模拟器中工作正常,但在我的设备上没有)。经过一些调试后,我意识到我的设备上锁定了肖像取向,导致UIDeviceOrientationDidChangeNotification不能触发。不知道这是你的问题,但这是容易忽视和值得检查的事情。