在这段代码中:
NSArray *supportedOrientations = nil;
if( iPhone ) { // bool
supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];
// one array element
}
else if( iPad ) { // bool
supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations~ipad"];
// returns nil
}
else {
NSLog(@"%@:%@ device type not identified", kClassName, kMethodName);
}
如果设备是 iPhone,supportedOrientations 有数组。
如果是 iPad,supportedOrientations 为零。
该文件总是被找到,因此 NSLog 永远不会显示(通过调试器逐步确认)。
使用文本编辑检查 plist 时,我看到:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
关于为什么会发生这种情况的任何想法?
在 iOS 模拟器 4.3 版中运行。
最佳答案
当一个包加载它的infoDictionary
,它检查字典中的每个键。如果 key 是特定于设备的,则捆绑包会检查该 key 是否引用当前设备。
UISupportedInterfaceOrientations~iPad
至UISupportedInterfaceOrientations
. UISupportedInterfaceOrientations~iPad
字典中的键。 所以你可以这样做:
NSArray *supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];
这将在所有设备上做正确的事情。
你可以这样做,它会更短一点:
NSArray *supportedOrientations = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UISupportedInterfaceOrientations"];
关于iphone - 无法获得支持的界面方向数组,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8370166/