MrH*_*Hen 1 iphone function-pointers objective-c backwards-compatibility ipad
我正在开发一款能够在iPad和iPhone上运行的通用应用程序.苹果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;
}
}
Run Code Online (Sandbox Code Playgroud)
在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;
}
}
Run Code Online (Sandbox Code Playgroud)
这有效,但会导致编译器警告:"指针与整数之间的比较".在挖掘之后,我发现我可以通过以下转换使编译器警告消失(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;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:这里的最后一个代码块是否正常/可接受/标准练习?我找不到其他人做这样的事情快速搜索,这让我想知道我是否错过了一个陷阱或类似的东西.
谢谢.
您需要针对3.2 SDK构建适用于iPad的应用程序.因此它将正确构建,UI_USER_INTERFACE_IDIOM()宏仍然可以工作.如果你想知道如何/为什么,请在文档中查找它 - 它是一个#define,它将被编译器理解并编译成可在3.1(等)上正确运行的代码.
| 归档时间: |
|
| 查看次数: |
3098 次 |
| 最近记录: |