检查运行时是否存在UI_USER_INTERFACE_IDIOM [英] Checking whether UI_USER_INTERFACE_IDIOM exists at runtime

查看:256
本文介绍了检查运行时是否存在UI_USER_INTERFACE_IDIOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款能够在iPad和iPhone上运行的通用应用。苹果iPad文档说使用 UI_USER_INTERFACE_IDIOM()检查我是否在iPad或iPhone上运行,但我们的iPhone是3.1.2并且没有 UI_USER_INTERFACE_IDIOM()已定义。因此,此代码中断:

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;
 }
}

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

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;
 }
}

这样可行,但会导致编译器警告:指针和整数之间的比较。在挖掘之后,我发现我可以通过以下转换使编译器警告消失(void *)

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.

谢谢。

推荐答案

您需要针对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).

这篇关于检查运行时是否存在UI_USER_INTERFACE_IDIOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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