Refreshing Parent ViewController after dismissing ModalViewController

后端 未结 3 759
隐瞒了意图╮
隐瞒了意图╮ 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 01:42

    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:

    @protocol MediaPreviewViewControllerDelegate <NSObject>
    
    -(void)didRemovedImage;
    
    @end
    
    @interface MediaPreviewViewController : NSObject {
        id< MediaPreviewViewControllerDelegate > delegate;
    }
    
    @property (nonatomic, assign) id < MediaPreviewViewControllerDelegate> delegate;
    

    In MediaPreviewViewController.m:

    @synthesize delegate = _delegate;
    

    Then in your method in your MediaPreviewViewController where you remove the image, you just call:

    [_delegate didRemoveImage];
    

    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

    0 讨论(0)
  • 2021-02-02 01:57

    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:

    -(IBAction)choosePic:(id)sender
    {
        UIButton *picButton = (UIButton *)sender;
    
        // Set PopoverPresentation view controller
    
        PicViewController *picVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PicViewController"];
    
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:picVC];
    
        picVC.preferredContentSize = CGSizeMake(500., 500.);
    
    
        // Set this function as described in Allan's answer
    
        picVC.dismissPopover = ^(UIViewController *controller, UIImage *image) 
        {
    
           _myImage = image;
    
           .....
    
           .....
    
           .....
    
        };
    
        navController.modalPresentationStyle = UIModalPresentationPopover;
    
        _picPopover = navController.popoverPresentationController;
    
        _picPopover.delegate = self;
    
        _picPopover.sourceView = self.view;
    
        _picPopover.sourceRect = [picButton frame];
    
        navController.modalPresentationStyle = UIModalPresentationPopover;
    
        navController.navigationBarHidden = YES;
    
        [self presentViewController:navController animated:YES completion:nil];
    
    }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题