Refreshing Parent ViewController after dismissing ModalViewController

后端 未结 3 761
隐瞒了意图╮
隐瞒了意图╮ 2021-02-02 01:31

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 t

3条回答
  •  粉色の甜心
    2021-02-02 02:03

    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
    

提交回复
热议问题