7

按照標題。 調用[[UIDevice currentDevice] BeginGeneratingDeviceOrientationNotifications]不起作用。UIDevice currentDevice的「方向」始終爲空

DidRotateToInterfaceOrientation等事件工作正常,但我需要能夠任意調查設備方向。

如何修復/執行此操作?

長篇小說: 我在每個選項卡上都有一個帶有導航控制器的選項卡應用程序。第一個標籤的根視圖是當方向改變爲橫向時全屏顯示的圖形;然而,每當視圖出現時需要檢查,因爲方向改變可能發生在其他地方,所以我希望在出現這個視圖時輪詢定向狀態。

回答

0

不會[[UIDevice currentDevice]方向]給你當前的方向狀態嗎?您可以在您想要輪詢的視圖控制器的viewWillAppear方法中檢查這一點。

編輯:除此之外,有多種獲取當前方向的方法,例如在UIApplication中使用statusBarOrientation屬性,或在UIViewcontroller中使用interfaceOrientation屬性。

+0

看起來像你沒看過這個問題。他說即使他調用beginGeneratingOrientationNotifications,該方法仍然返回NULL。 – Jasarien 2010-05-23 21:29:40

2

似乎是一個愚蠢的問題,不過,是不是

beginGeneratingDeviceOrientationNotifications

(小寫B) ...

9

方向的UIDevice的概念似乎是唯一可用在實際設備上。模擬器似乎總是在這裏返回0,無論通知是否按照文檔提示啓用。刺激不方便,但你去了。

我覺得這是正常工作的實際設備上:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    NSLog(@"orientation: %d", [[UIDevice currentDevice] orientation]); 
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; 
+1

是的,只是浪費了2h試圖找出什麼是錯的,但它確實只是模擬器。所有在設備上工作正常。 – samvermette 2010-11-12 02:56:21

+3

[[UIDevice currentDevice]方向]在用戶從Springboard鎖定設備方向時不起作用。方向始終是最後一個值,並且沒有更改通知。 – Jeff 2011-07-16 01:37:53

+5

UIDeviceOrientation interfaceOrientation = [UIApplication sharedApplication] .statusBarOrientation; 這個替代方案爲我工作 – iMeMyself 2012-12-11 11:55:51

1

如果檢查[[UIDevice currentDevice] orientation]- (void)viewDidLoad你總是會得到零。

檢查它在* - (void) viewDidAppear:(BOOL) *動畫的方法和你

+1

「...你總是會得到'nil'」。這是記錄的嗎? – 2014-02-05 16:54:27

1

這是因爲每iMeMyself在上面的評論說 - 這個SAMED了我很多時間,我認爲是正確的答案,所以我想強調它在這裏:

UIDeviceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation; 

if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) 
{ 
    //do stuff here 
} 
else if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) 
{ 
//or do stuff here 
} 
+0

這對我有效,但不應該是'UIInterfaceOrientation interfaceOrientation = [....'? – Jimmery 2013-08-13 09:50:06

+0

針對以上使用情況進行了更新 – craigk 2013-08-14 04:03:32