2011-04-17 139 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不能觸發。不知道這是你的問題,但這是容易忽視和值得檢查的事情。