In my iOS app, a user can select an image from a list, upon which they are presented with a modal that contains the image and options to delete the image. If the user chooses to delete the image, she is returned to the original viewController containing the list of images. I need to then refresh the original ViewController to take into account the deleted image.
I tried using NSNotificationCenter to broadcast when an image is deleted to the parent View Controller. However, it seems like the broadcast is never received.
Is there some other way to
- send data back to the parent ViewController after the modal is dismissed, and
- detect when the modal is dismissed from the parent ViewController?
(I tried following the example outlined here, but it didn't seem to work)
Below is my code:
EditStepViewController (original View Controller):
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MediaPreviewViewController *mediaPreviewVC = (MediaPreviewViewController *)[storyboard instantiateViewControllerWithIdentifier:@"MediaPreviewViewController"];
mediaPreviewVC.selectedImageURL = [NSString stringWithFormat:@"%@",gestureRecognizer.view.tag];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mediaPreviewVC];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didDismissMediaPreview)
name:@"MediaPreviewDismissed"
object:nil];
[self presentViewController:navigationController animated:YES completion:nil];
MediaPreviewViewController (second ViewController):
...
[self deleteImage];
[[NSNotificationCenter defaultCenter] postNotificationName:@"MediaPreviewDismissed" object:nil userInfo:nil];
[self dismissViewControllerAnimated:YES completion:^(){
NSLog(@"dismissed controller");
}];
Then, back in EditStepViewController:
-(void)didDismissMediaPreview{
NSLog(@"dismissed media preview"); // this is never logged!
[self.view setNeedsDisplay]; // refresh view to account for deleted image
}
Thanks in advance for your help!
You should make a protocol in your MediaPreviewViewController. Then when the image is deleted, send a delegate method so the parent viewcontroller can handle that.
You also should never let the viewcontroller dismiss itself (though it is possible, but recommended that the view which created the modal is also responsible for removing the modal...)
So you should get something like this:
In MediaPreviewViewController.h:
In MediaPreviewViewController.m:
Then in your method in your MediaPreviewViewController where you remove the image, you just call:
In your parent viewcontroller, you need to implement this protocol like you are used to with delegates.. You then can also remove the view from the parent in this delegate method
This may help anyone who have same problem on UIPopovePresentationController. From my experience, Allan's answer can resolve the same issue too.
I've just have problem about Viewcontroller's delegate that present as UIPopoverPresentingController didn't send call to its root view. And Allan's answer can solved this fit.
My sample code:
In my case I usually use block here.
For example you have ParentViewController.h
Implementation ParentViewController.m
And, ModalViewController.h
ModalViewController.m