我正在制作一个通用应用程序,它在 iPhone 上运行良好!但在 iPad 上,它无法拉起图像选择器。代码是:
- (IBAction)openImagePicker:(id)sender //Makes UIImagePicker roll up from the bottom.
{
UIActionSheet *alertSheet = [[UIActionSheet alloc] initWithTitle:@"Where do you want to get your daily image?" delegate:(self) cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Library", nil];
[alertSheet setTag:0];
[alertSheet setDelegate:self];
[alertSheet showFromTabBar:[[self tabBarController] tabBar]];
[alertSheet release];
}
它说原因是“* 由于未捕获的异常‘NSInvalidArgumentException’而终止应用程序,原因:‘在 iPad 上,UIImagePickerController 必须通过 UIPopoverController 呈现’”我该怎么做?感谢您的帮助。
最佳答案
您必须检查应用程序安装在哪种类型的设备上,然后适本地显示 Controller 。您可以按照以下方式执行操作:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
// We are using an iPhone
UIActionSheet *alertSheet = [[UIActionSheet alloc] initWithTitle:@"Where do you want to get your daily image?" delegate:(self) cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Library", nil];
[alertSheet setTag:0];
[alertSheet setDelegate:self];
[alertSheet showFromTabBar:[[self tabBarController] tabBar]];
[alertSheet release];
}else {
// We are using an iPad
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
UIPopoverController *popoverController=[[UIPopoverController alloc] initWithContentViewController:imagePickerController];
popoverController.delegate=self;
[popoverController presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
不要忘记实现 UIPopoverController 委托(delegate)方法。
祝你好运
关于objective-c - 如何使用 UIPopOverController 制作一个在 iPhone 上使用 ImagePicker 的 iPhone 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8290966/