Objective c 如何使用UIPopOverController使使用ImagePicker的iPhone应用程序在iPhone上工作?

Objective c 如何使用UIPopOverController使使用ImagePicker的iPhone应用程序在iPhone上工作?,objective-c,ios,cocoa-touch,ipad,uipopovercontroller,Objective C,Ios,Cocoa Touch,Ipad,Uipopovercontroller,我正在制作一个通用应用程序,它在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 i

我正在制作一个通用应用程序,它在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呈现’”。我该怎么做?谢谢您的帮助。

您必须检查应用程序安装在哪种类型的设备上,然后适当地显示控制器。您可以按照以下方式进行操作:

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委托方法


祝你好运

你需要检查iPad的代码并显示一个popover,否则只需显示图像控制器即可。