2013-09-27 64 views
0

我按比特有點失落;)如何使UIInterfaceOrientation枚舉測試值

我的目標是要獲取一整套通過應用支撐取向和測試每個結果值更新自定義變量。 我的問題是,我不知道怎麼做的比較(我有一個轉換/測試問題...)

首先,我讀這篇文章:Testing for bitwise Enum values 但是它不帶我光.. 。

讓說我有以下的方向申報我的應用程序(以下是我的變量supportedOrientations日誌輸出): 支撐取向=( UIInterfaceOrientationPortrait )

所以我的第一次嘗試是嘗試整數值一些測試,但它不工作(即使應用程序被宣佈爲在縱向模式下測試返回「假」):

NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"]; 
NSLog(@"[supported orientations = %@", supportedOrientations); 
// for clarity just make a test on the first orientation we found 
if ((NSInteger)supportedOrientations[0] == UIInterfaceOrientationPortrait) { 
    NSLog(@"We detect Portrait mode!"); 
} 

我的第二次嘗試嘗試按位的事情但這次它總是返回'真'(即使支持的方向不是UIInterfaceOrientationPortrait)。 :

NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"]; 
NSLog(@"[supported orientations = %@", supportedOrientations); 
// for clarity just make a test on the first orientation we found 
if ((NSInteger)supportedOrientations[0] | UIInterfaceOrientationPortrait) { // <-- I also test with UIInterfaceOrientationMaskPortrait but no more success 
    NSLog(@"We detect Portrait mode!"); 
} 

所以我的問題是:

  • 如何測試方向在我的情況?

  • 這是一種通過使用按位事件(使用|操作數)來使用測試的方法嗎?

回答

0

官方文檔說,UISupportedInterfaceOrientations字符串數組的。 https://developer.apple.com/library/ios/documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html#//apple_ref/doc/uid/TP40009252-SW10

所以解決的辦法是對數組中的每個元素使用NSString比較。

NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"]; 
for (NSString *orientation in supportedOrientations) {   
    if ([orientation isEqualToString:@"UIInterfaceOrientationPortrait"] || 
     [orientation isEqualToString:@"UIInterfaceOrientationPortraitUpsideDown"]) { 
     NSLog(@"*** We detect Portrait mode!"); 
    } else if ([orientation isEqualToString:@"UIInterfaceOrientationLandscapeLeft"] || 
       [orientation isEqualToString:@"UIInterfaceOrientationLandscapeRight"]) { 
     NSLog(@"*** We detect Landscape mode!"); 
    } 
} 

注意,做這樣的,我們沒有利用枚舉值(類型UIInterfaceOrientation的),但它的作品!