Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 598
Joseph800 Avatar asked Jan 29 '26 06:01

Joseph800


1 Answers

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

like image 184
timthetoolman Avatar answered Jan 30 '26 18:01

timthetoolman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!