views:

787

answers:

2

After I choose a picture through the UIImagePickerController interface from the Photo Library, the Photo Library view stays displayed, even though I've called dismissModelViewControllerAnimated in imagePickerController:didFinishPickingImage:editingInfo.

Has anyone seen this? These are the three relevant methods I'm using:

- (IBAction)choosePictureFromLibrary:(id)sender {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
     UIImagePickerController* picker = [[UIImagePickerController alloc] init];
     picker.delegate = self;
     picker.allowsImageEditing = YES;
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
     [self presentModalViewController:picker animated:YES];
     [picker release];
    }
    else {
     UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error accessing Photo Library" message:@"This device does not support a Photo Library." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [alert show];
     [alert release];
    }
}


- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo {   
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Picture picked!" message:@"You picked a picture!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

    [picker dismissModalViewControllerAnimated:YES];
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker {   
    [picker dismissModalViewControllerAnimated:YES];
}

I would have thought that calling imagePickerController:didFinishPickingImage:editingInfo would completely dismiss the Photo Library view, but it doesn't seem to. Is there anything else I have to do to make it go away?

+4  A: 

You need to access the viewController of the picker not the picker itself. Try this line instead.

[[picker parentViewController] dismissModalViewControllerAnimated:YES];
That did the trick! Thank you so much, this has been driving me crazy!
unforgiven3
Thanks! Ha ha. This, too, was killing me!
LucasTizma
+2  A: 

You can just call

[self dismissModalViewControllerAnimated:YES];

to dismiss any modal view controller on top of the current view.

This makes sense since you present the view controller by calling:

[self presentModalViewController:picker animated:YES];
Frank Szczerba