1

我正在研究应该能够在iPad和iPhone上运行的通用应用程序。 Apple iPad文档说要使用UI_USER_INTERFACE_IDIOM()来检查我是否在iPad或iPhone上运行,但是我们的iPhone是3.1.2,并且不会定义UI_USER_INTERFACE_IDIOM()。因此,该代码就会中断:检查在运行时是否存在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; 
} 
} 

在苹果的SDK Compatibility Guide他们建议做以下检查是否函数存在:

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

我的问题是:是最后一个代码块在这里好/可接受/标准的做法?我无法找到任何其他人正在做这样的快速搜索,这让我想知道我是否错过了一个陷阱或类似的东西。

谢谢。

+0

'UI_USER_INTERFACE_IDIOM'是一个编译时宏。它在运行时不“存在” – user102008 2011-03-12 00:37:41

+0

这不会使这个问题值得赞成。 – 2011-12-14 18:01:11

回答

6

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

+0

好的,是的,工作。我想清楚发生了什么事:我最初设置了两个不同的应用程序,然后切换到使用通用应用程序。第一块中的代码是用于旧的方式,而我显然从3.2开始就没有运行它。谢谢您的帮助! – MrHen 2010-04-16 16:11:37