运行时检查 UI_USER_INTERFACE_IDIOM 是否存在

发布于 2024-08-29 00:46:08 字数 1846 浏览 9 评论 0 原文

我正在开发一款通用应用程序,应该能够在 iPad 和 iPhone 上运行。 Apple iPad 文档说使用 UI_USER_INTERFACE_IDIOM() 来检查我是否在 iPad 或 iPhone 上运行,但我们的 iPhone 是 3.1.2 并且不会定义 UI_USER_INTERFACE_IDIOM() 。因此,此代码中断:

//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  return YES; //are we on an iPad?
 } else {
  return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
 }
}

在Apple的 SDK 兼容性指南 他们建议执行以下操作来检查该函数是否存在:

//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 if(UI_USER_INTERFACE_IDIOM() != NULL &&
  UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  return YES; //are we on an iPad?
 } else {
  return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
 }
}

这有效,但会导致编译器警告:“指针和整数之间的比较。”经过深入研究后,我发现我可以通过以下转换为 (void *) 使编译器警告消失:

//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 if((void *)UI_USER_INTERFACE_IDIOM() != NULL &&
  UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  return YES; //are we on an iPad?
 } else {
  return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
 }
}

我的问题是:这里的最后一个代码块可以/可接受/标准做法吗?我找不到其他人通过快速搜索做类似的事情,这让我想知道我是否错过了陷阱或类似的事情。

谢谢。

I am working on a universal app that should be able to run on iPad and iPhone. The Apple iPad docs say to use UI_USER_INTERFACE_IDIOM() to check if I am running on iPad or iPhone, but our iPhone is 3.1.2 and will not have UI_USER_INTERFACE_IDIOM() defined. As such, this code breaks:

//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  return YES; //are we on an iPad?
 } else {
  return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
 }
}

In Apple's SDK Compatibility Guide they suggest doing the following to check if the function exists:

//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 if(UI_USER_INTERFACE_IDIOM() != NULL &&
  UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  return YES; //are we on an iPad?
 } else {
  return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
 }
}

This works, but results in the compiler warning: "Comparison between pointer and integer." After digging around I figured out that I can make the compiler warning disappear with the following cast to (void *):

//iPhone should not be flipped upside down. iPad can have any
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 if((void *)UI_USER_INTERFACE_IDIOM() != NULL &&
  UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  return YES; //are we on an iPad?
 } else {
  return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
 }
}

My question is this: Is the last code block here okay/acceptable/standard practice? I couldn't find anyone else doing something like this with quick searching which makes me wonder if I missed a gotcha or something similar.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还如梦归 2024-09-05 00:46:08

您需要根据 3.2 SDK 构建 iPad 应用程序。因此,它将正确构建,并且 UI_USER_INTERFACE_IDIOM() 宏仍然有效。如果您想知道如何/为什么,请在文档中查找它 - 它是一个 #define,它将被编译器理解并编译成可以在 3.1(等)上正确运行的代码。

You need to build apps for iPad against the 3.2 SDK. As such it will build correctly, and the UI_USER_INTERFACE_IDIOM() macro will still work. If you want to know how/why, look it up in the docs - it is a #define, which will be understood by the compiler and compile into code that will run correctly on 3.1 (etc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文