Refreshing Parent ViewController after dismissing ModalViewController

后端 未结 3 758
隐瞒了意图╮
隐瞒了意图╮ 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 
    
    -(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

提交回复
热议问题