Graphics Programs Reference
In-Depth Information
ler:didFinishPickingMediaWithInfo:
, dismiss the popover when an image is
selected.
[imageView setImage:image];
[self dismissViewControllerAnimated:YES completion:nil];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
// If on the phone, the image picker is presented modally. Dismiss it.
[self dismissViewControllerAnimated:YES completion:nil];
} else {
// If on the pad, the image picker is in the popover. Dismiss the popover.
[imagePickerPopover dismissPopoverAnimated:YES];
imagePickerPopover = nil;
}
}
When you explicitly send the message
dismissPopoverAnimated:completion:
to dismiss the popover controller, it does not send
popoverControllerDidDis-
missPopover:
to its delegate, so you must set
imagePickerPopover
to
nil
in
dismissPopoverAnimated:completion:
.
There is a small bug to fix. If the
UIPopoverController
is visible and the user taps
on the camera button again, the application will crash. This crash occurs because the
UIPopoverController
that is on the screen is destroyed when
imagePicker-
Popover
is set to point at the new
UIPopoverController
in
takePicture:
.
You can ensure that the destroyed
UIPopoverController
is not visible and cannot
be tapped by adding the following code to the top of
takePicture:
in
De-
tailViewController.m
.
- (IBAction)takePicture:(id)sender
{
if ([imagePickerPopover isPopoverVisible]) {
// If the popover is already up, get rid of it
[imagePickerPopover dismissPopoverAnimated:YES];
imagePickerPopover = nil;
return;
}
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
Build and run the application. Tap the camera button to show the popover and then tap it
again - the popover will disappear.