2012-07-08 32 views
0

這是關於從一個簡單的UIInterfaceOrientation對象返回值一個相當基本的問題,我試試這個代碼:UIInterfaceOrientation對象的返回值等於布爾型var?

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation 
{ 
BOOL orientacion = interfaceOrientation; 
return orientacion; 
} 

和轉換這樣做,所以我想一個UIInterfaceOrientation對象是等於一個布爾值VAR?是一個隱式的錯字或真正的UIInterfaceOrientation等於一個布爾值..

回答

5

UIInterfaceOrientation是一個enum,這基本上意味着它是一個整數。整數可以分配給布爾值。許多事情可以 - 布爾人簡單地等同於真或假。如果布爾值設置爲0nil,則該值爲false。如果它設置爲任何其他以外的其他0nil(或某些其他#defined等價物),它將是真的。由於UIInterfaceOrientation是一個枚舉(整數),如果它等於0,布爾值將爲false。如果它不是0,那將是真的。

UIInterfaceOrientation值:

typedef enum { 
    UIDeviceOrientationUnknown, 
    UIDeviceOrientationPortrait,   // Device oriented vertically, home button on the bottom 
    UIDeviceOrientationPortraitUpsideDown, // Device oriented vertically, home button on the top 
    UIDeviceOrientationLandscapeLeft,  // Device oriented horizontally, home button on the right 
    UIDeviceOrientationLandscapeRight,  // Device oriented horizontally, home button on the left 
    UIDeviceOrientationFaceUp,    // Device oriented flat, face up 
    UIDeviceOrientationFaceDown    // Device oriented flat, face down 
} UIDeviceOrientation; 

此列表中的第一個將等於0。接下來的1,接下來的2等。所以UIDeviceOrientationUnknown將布爾值設置爲false;其他任何東西都會將其設置爲true。


在任何情況下,您都沒有正確使用此功能。這個函數中的代碼需要閱讀:

if((interfaceOrientation == someOrientationYouWantToWork) || (interfaceOrientation == someOtherOrientationYouWantToWork) 
{ 
    return YES; 
} 
else 
{ 
    return NO; 
} 

設置someOrientationYouWantToWork等從我上面張貼的枚舉值。無論您想要工作的方向如何,請返回YES。否則它將返回NO

+0

yes我知道我提供的示例是一個不好的用法,現在我使用這個:if(interfaceOrientation!= UIInterfaceOrientationPortrait && interfaceOrientation!= UIInterfaceOrientationPortraitUpsideDown)orientation = true; } else { orientation = false; }返回方向; //同樣的:return UIInterfaceOrientationIsLandscape(interfaceOrientation); – xleonhart 2012-07-08 07:16:07

+0

不錯的答案,小錯誤:你正在列出UIDeviceOrientation的值。 UIInterfaceOrientation可以在這裏找到:https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIInterfaceOrientation – katzenhut 2014-03-20 09:04:52

1

它不是一個布爾值,而是一個枚舉值 - 如果它不是0,它默認爲布爾型「是」,否則它是「沒有」。