Joseph800
Joseph800

Reputation: 101

How do I use UIPopOverController to make an iPhone App that uses the ImagePicker work on iPhone?

I am making a universal app, and it works great on the iPhone! But on the iPad, it cannot pull up the image picker. The code is:

- (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];
}

It says the reason is "* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'" How do I do this? Thank you for your help.

Upvotes: 2

Views: 7720

Answers (1)

timthetoolman
timthetoolman

Reputation: 4623

You will have to check for which type of device idiom the app is installed on and then present the controller appropriately. You can do something along the lines of the following:

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];
}

don't forget to implement the UIPopoverController delegate methods.

Good Luck

Upvotes: 12

Related Questions