Search code examples
iosipaduiimagepickercontrolleruisplitviewcontroller

iOS Imagepicker on iPad with a SplitViewController


I have a split view with a master table view and a detail view. On the navigation bar of my master view of my split view I have a button that should enable the user to take a picture via imagePicker. But my program keeps crashing when I hit the button. Here's the procedure of the MasterViewController.m that is called when the button is pushed:

-(void) takePicture:(id) sender
{

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
}
else
{
    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}

[imagePicker setDelegate:self];

//[self presentViewController:imagePicker animated:YES completion:nil];

if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    UIPopoverController * popOverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
    [popOverController presentPopoverFromRect:((UIButton *)sender).frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    //[popOverController presentPopoverFromRect:CGRectMake(700, 1000, 10, 10) inView:self.detailViewController.imageSpace permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

}else {

    [self presentViewController:imagePicker animated:YES completion:nil];
}

}

and here's the error message:

2013-02-11 09:06:43.975 ImageSplit2[13044:c07] -[UIBarButtonItem frame]: unrecognized selector sent to instance 0x7183d00
2013-02-11 09:06:43.976 ImageSplit2[13044:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIBarButtonItem frame]: unrecognized selector sent to instance 0x7183d00'

Solution

  • sender is not a UIButton, it is a UIBarButtonItem which is not a view so it doesn't have a frame property.

    Change this:

    [popOverController presentPopoverFromRect:((UIButton *)sender).frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    

    to:

    [popOverController presentPopoverFromBarButtonItem:(UIBarButtonItem *)sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    

    Side note: You must use a popover on the iPad if selecting an image from the photo library. However, it is perfectly valid to present a camera based image picker as a full screen view controller. So you may wish to change your if statement from:

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    

    to:

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && imagePicker.sourceType != UIImagePickerControllerSourceTypeCamera)