我正在研究应该能够在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;
}
}
我的问题是:是最后一个代码块在这里好/可接受/标准的做法?我无法找到任何其他人正在做这样的快速搜索,这让我想知道我是否错过了一个陷阱或类似的东西。
谢谢。
'UI_USER_INTERFACE_IDIOM'是一个编译时宏。它在运行时不“存在” – user102008 2011-03-12 00:37:41
这不会使这个问题值得赞成。 – 2011-12-14 18:01:11