2013-09-27 61 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的),但它的作品!