Gonghan
Gonghan

Reputation: 287

ViewDidAppear not called when the modal view is dismissed

First, I create a MainViewController. Then in MainViewController, I do

[self presentViewController:modalViewController animated:YES completion:nil];
modalViewController.modalPresentationStyle = UIModalPresentationFormSheet;

When I dismiss the modalViewController, On iPhones(except iPhone 6+), viewDidAppear of MainViewController is called. On iPads and iPhone 6+, viewDidAppear of MainViewController is not called.

The logic is to called a function when the modalViewController is dismissed. How can I know when the modalViewController is dismissed.

Upvotes: 8

Views: 13324

Answers (2)

DanielG
DanielG

Reputation: 2377

You can use a delegate to call your function in MainViewController when you dismiss the modal view controller. For example:

MainViewController.h:

@protocol YourDelegate <NSObject>
- (void)someFunction;
@end

@interface MainViewController : UIViewController <YourDelegate>

@end

MainViewController.m:

// Where you present the modal view
ModalViewController *view = [[ModalViewController alloc] init];
view.delegate = self;
[self presentViewController:view animated:YES completion:nil];

ModalViewController.h:

@interface ModalViewController : UIViewController
@property (nonatomic, weak) id<YourDelegate> delegate;
@end

ModalViewController.m

// Wherever you dismiss..
[self dismissViewControllerAnimated:YES completion:^{
    [self.delegate someFunction];
}

Upvotes: 7

Dimitri Bouniol
Dimitri Bouniol

Reputation: 766

The way Apple supplied view controllers do this is to have a delegate on the presented view controller, that is called when that view controller requests to be closed. Then, the presenter would be responsible for dismissing the controller, and would thus also know when to do any associated clean up (both before and after the animation).

Upvotes: 3

Related Questions