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

Jos*_*800 2 cocoa-touch objective-c ipad uipopovercontroller ios

我正在制作一个通用的应用程序,它在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];
}
Run Code Online (Sandbox Code Playgroud)

它说的原因是" *由于未捕获的异常'终止应用'NSInvalidArgumentException',原因:'在iPad上,UIImagePickerController必须通过UIPopoverController呈现'"我该怎么做?谢谢您的帮助.

tim*_*man 12

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

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];
}
Run Code Online (Sandbox Code Playgroud)

不要忘记实现UIPopoverController委托方法.

祝好运