UIView notification when modal UIImagePickerController is dismissed?

后端 未结 3 1623
天涯浪人
天涯浪人 2021-01-27 00:50

Is there a way to call code when a modal view is finished dismissing?

EDIT:

I\'m sorry, I didn\'t clarify earlier. I\'m trying to dismiss a UIImagePickerControll

3条回答
  •  离开以前
    2021-01-27 01:38

    You use a delegate pattern for the modal view to inform whoever presented it when it's finished.

    MyModalViewController.h:

    @protocol MyModalViewControllerDelegate;
    
    @interface MyModalViewController : UIViewController
    {
        id delegate;
    }
    
    @property (nonatomic, assign) id delegate;
    
    @end
    
    
    @protocol MyModalViewControllerDelegate
    - (void)myModalViewControllerFinished:(MyModalViewController*)myModalViewController;
    @end
    

    MyModalViewController.m:

    @synthesize delegate;
    
    // Call this method when the modal view is finished
    - (void)dismissSelf
    {
        [delegate myModalViewControllerFinished:self];
    }
    

    ParentViewController.h:

    #import "MyModalViewController.h"
    
    @interface ParentViewController : UIViewController 
    {
    }
    

    ParentViewController.m:

    - (void)presentMyModalViewController
    {
        MyModalViewController* myModalViewController = [[MyModalViewController alloc] initWithNibName:@"MyModalView" bundle:nil];
        myModalViewController.delegate = self;
        [self presentModalViewController:myModalViewController animated:YES];
        [myModalViewController release];
    }
    
    - (void)myModalViewControllerFinished:(MyModalViewController*)myModalViewController
    {
        [self dismissModalViewControllerAnimated:YES];
    }
    

    EDIT:

    I haven't used UIImagePickerController, but looking at the docs, it looks like you already have most of the code done for you, as there is an existing UIImagePickerControllerDelegate class that has three different "dismissal" delegate callbacks (although one is deprecated). So you should make your ParentViewController class (whatever that is) implement the UIImagePickerControllerDelegate pattern and then implement those methods. While each method will do something different (since you have to handle when the user actually selects an image, or if they cancel), they each will do the same thing at the end: call dismissModalViewControllerAnimated: to dismiss the picker.

提交回复
热议问题