objective-c - iPhone 和 iPad 的条件编码在现实中是如何工作的?

标签 objective-c ios conditional-statements uiimagepickercontroller uipopovercontroller

UIImagePicker 在 iPhone 和 iPad 上的呈现方式必须不同。在 iPad 上,它会抛出一个异常:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'

所以我必须将代码添加到我的 iPad 专用通用应用程序中。有什么安全的方法可以让应用程序不会在缺少 UIPopoverController 的设备上崩溃?

例子:

popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
        [popover presentPopoverFromRect:CGRectMake(100, 100.0, 0.0, 0.0) 
                                 inView:self.view
               permittedArrowDirections:UIPopoverArrowDirectionAny 
                               animated:YES];

此外,如果我需要一个 UIPopoverController,我需要一个实例变量来保存它。我该如何处理?

最佳答案

您可以通过执行此操作来检查类是否存在。

Class popoverClass = (NSClassFromString(@"UIPopoverController"));
if (popoverClass != nil) {
// you're on ipad
} else {
// you're on iphone/ipod touch
}

关于objective-c - iPhone 和 iPad 的条件编码在现实中是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6112450/

相关文章:

ios - segues的自动化测试

ios - 为特定日期/时间安排选择器

ios - 在不同状态下接收推送通知

mysql - SQL Server + 通过具有条件进行分组

R:使用条件语句创建一个新的df(与第n行和第n-1行相关)

JavaScript 和复选框

objective-c - 在 Objective-C 中解压缩 gzip 数据流

ios - 元 "apple-itunes-app"不起作用

iphone - 使用 CoreTelephony 在 iPhone 上获取 IMEI?

ios - Cocoa Touch 框架中 Unresolved 公共(public)类