Refreshing Parent ViewController after dismissing ModalViewController

scientiffic picture scientiffic · Sep 2, 2014 · Viewed 8.3k times · Source

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

  1. send data back to the parent ViewController after the modal is dismissed, and
  2. 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!

Answer

Allan  Macatingrao picture Allan Macatingrao · Sep 2, 2014

In my case I usually use block here.

For example you have ParentViewController.h

@interface ParentViewController : UIViewController
@end

Implementation ParentViewController.m

// INCLUDE HERE THE MODAL CONTROLLER TO HAVE ACCESS TO ITS PUBLIC PROPERTY
#import ModalViewController.h

@implementation ParentViewController

// implement your modal dismiss block here
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   // DEFINE HERE THE CALLBACK FUNCTION
   // 1. get the model view controller
   ModalViewController *mvc = [segue destinationViewController];

   // 2. Your code after the modal view dismisses
   mvc.onDismiss = ^(UIViewController *sender, NSObject *objectFromModalViewController)
   {
       // Do your stuff after dismissing the modal view controller
       .
       .
       .
   }
} 
@end

And, ModalViewController.h

@interface ModalViewController : UIViewController

// call back function, a block
@property (nonatomic, strong) void (^onDismiss)(UIViewController *sender, NSObject *objectYouWantToPassBackToParentController)
@end

ModalViewController.m

@implementation ModalViewController

.
.
.

// your dismiss function say
- (IBAction)dismissViewController:(id)sender
{
   ... 

   [self deleteImage];

   [self dismissViewControllerAnimated:YES completion:^
   {
      // MAKE THIS CALL
      self.onDismiss(self, theOjectYouWantToPassBackToParentVC);
   }];
}
@end