1

我正在开发一个应该能够在 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;
 }
}

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

谢谢。

4

1 回答 1

6

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

于 2010-04-16T16:05:26.107 回答