Graphics Programs Reference
In-Depth Information
[imagePicker setDelegate:self];
[self presentViewController:imagePicker animated:YES completion:nil];
// Place image picker on the screen
// Check for iPad device before instantiating the popover controller
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
// Create a new popover controller that will display the imagePicker
imagePickerPopover = [[UIPopoverController alloc]
initWithContentViewController:imagePicker];
[imagePickerPopover setDelegate:self];
// Display the popover controller; sender
// is the camera bar button item
[imagePickerPopover presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
} else {
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
Notice that we check for the device before creating the
UIPopoverController
. It is
critical to do this. You can only instantiate
UIPopoverController
s on the iPad fam-
ily of devices, and trying to create one on an iPhone will throw an exception.
Build and run the application on the iPad simulator or on an iPad. Navigate to the
De-
tailViewController
and tap the camera icon. The popover will appear and show
the
UIImagePickerController
's
view
. Select an image from the picker, and it
will appear in
DetailViewController
's
view
.
You can dismiss the popover controller by tapping anywhere else on the screen. When a
popover is dismissed in this way, it sends the message
popoverControllerDidDis-
missPopover:
to its delegate. Implement this method in
DetailViewControl-
ler.m
.
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverCon-
troller
{
NSLog(@"User dismissed popover");
imagePickerPopover = nil;
}
Notice that you set
imagePickerPopover
to
nil
here to destroy the popover. You
will create a new one each time the camera button is tapped.
The popover should also be dismissed when you select an image from the image picker. In
DetailViewController.m
, at the end of
imagePickerControl-