views:

44

answers:

2

Hi,

Lets say I have a controller class A which implements UIImagePickerControllerDelegate.

within A , I implement the delegate like this :

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [self dismissModalViewControllerAnimated:YES];  
}

Who will get dismissed here ? the imagePickerController or A ? please explain why...

A: 

Calling presentModalViewController:animated: on a UIViewController will display a view controller modally. By analogy, dismissModalViewControllerAnimated: dismisses the modal view controller. In this case, it would dismiss the image picker controller (Assuming the image picker controller is the modal view of the object that is its delegate, which is most likely the case).

anshuchimala
And how would you dismiss the modal view in case the object displayed the modal view and the delegate are not the same one ?can you then use instead "[picker dismissModalViewControllerAnimated]"instead of [self dismissModalViewControllerAnimated] ??
Idan
Not exactly. `[picker dismissModalViewControllerAnimated:]` means you're telling the `UIImagePickerController` to dismiss ITS modal view controller. What you would do is `[[picker parentViewController] dismissModalViewControllerAnimated:]`.
anshuchimala
Yes, however according to the answer below, the result is the same.Unless of course the modal view opened another modal view, which in that case I guess only the most inner one will get closed which is not one I intended.So I guess calling the parent specifically is the better option.
Idan
Oh, I see, I wasn't aware that the view controller would forward the message to its parent view controller. However, that would make your code harder to read and might inadvertently cause you to dismiss the wrong view controller, so you might as well just call it on the parent view controller directly.
anshuchimala
A: 

The UIImagePickerController will get dismissed.Here is the documentation

The parent view controller is responsible for dismissing the modal view controller it presented using the presentModalViewController:animated: method. If you call this method on the modal view controller itself, however, the modal view controller automatically forwards the message to its parent view controller.

vodkhang