如何让 UI_USER_INTERFACE_IDIOM() 与 iPhone OS SDK 一起工作 <3.2 [英] How does one get UI_USER_INTERFACE_IDIOM() to work with iPhone OS SDK < 3.2

查看:22
本文介绍了如何让 UI_USER_INTERFACE_IDIOM() 与 iPhone OS SDK 一起工作 <3.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple 建议使用以下代码来检测是在 iPad 还是 iPhone/iPod Touch 上运行:

Apple advises using the following code to detect whether running on an iPad or iPhone/iPod Touch:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  // The device is an iPad running iPhone 3.2 or later.
  // [for example, load appropriate iPad nib file]
}
else {
  // The device is an iPhone or iPod touch.
  // [for example, load appropriate iPhone nib file]
}

问题是 UI_USER_INTERFACE_IDIOM() 和 UIUserInterfaceIdiomPad 没有在 3.2 之前的 SDK 中定义.这似乎完全违背了这样一个功能的目的.它们只能在 iPhone OS 3.2 上编译和运行(iPhone OS 3.2 只能在 iPad 上运行).因此,如果您可以使用 UI_USER_INTERFACE_IDIOM(),结果将始终指示 iPad.

The problem is that UI_USER_INTERFACE_IDIOM() and UIUserInterfaceIdiomPad are NOT defined in the SDKs prior to 3.2. This seems to completely defeat the purpose of such a function. They can only be compiled and run on iPhone OS 3.2 (iPhone OS 3.2 can only be run on iPad). So if you can use UI_USER_INTERFACE_IDIOM(), the result will always be to indicate an iPad.

如果您包含此代码并针对 OS 3.1.3(最新的 iPhone/iPod Touch 操作系统)来测试您的 iPhone 绑定通用应用程序代码,您将收到编译器错误,因为这些符号未在 3.1 中定义.3 或更早版本,为 iPhone 模拟器 3.1.3 编译时.

If you include this code and target OS 3.1.3 (the most recent iPhone/iPod Touch OS) in order to test your iPhone-bound universal app code, you will get compiler errors since the symbols are not defined in 3.1.3 or earlier, when compiling for iPhone simulator 3.1.3.

如果这是 Apple 推荐的运行时设备检测方法,我做错了什么?有没有人成功地使用这种方法进行设备检测?

If this is the recommended-by-Apple approach to runtime device-detection, what am I doing wrong? Has anyone succeeded using this approach to device-detection?

推荐答案

我这样做是为了让代码在 3.1.3 和 3.2 中都能编译:

I do this to get the code to compile in both 3.1.3 and 3.2:

BOOL iPad = NO;
#ifdef UI_USER_INTERFACE_IDIOM
iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#endif
if (iPad) {
// iPad specific code here
} else {
// iPhone/iPod specific code here
}

我还在这里写了一篇关于它的快速博客文章:http://www.programbles.com/2010/04/03/compiling-conditional-code-in-universal-iphone-ipad-applications/

I also wrote a quick blog post about it here: http://www.programbles.com/2010/04/03/compiling-conditional-code-in-universal-iphone-ipad-applications/

这篇关于如何让 UI_USER_INTERFACE_IDIOM() 与 iPhone OS SDK 一起工作 <3.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆