Eric Brotto
Eric Brotto

Reputation: 54281

Present modal view controller - interact with presenting viewcontroller

When presenting a modal view controller how does one then interact with the parent view controller once the presented view controller is dismissed.

From what I understand viewWillAppear does not get called on the parent view controller when dismissing the modal view controller. How then does one update the UI based on the input taken in on the modal view controller?

Can the modal view controller call methods on it's parent view controller? i.e [self.parentViewController doWhatIWant];?

Or alternatively is there a method that gets called on the parent view controller when the modal view controller is dismissed?

Upvotes: 1

Views: 417

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

You can provide the modal controller with an instance of parent before displaying it modally, letting it call you back:

@class ParentViewController;

@interface ControllerToBeShownModally : UIViewController

@property (nonatomic, readwrite) ParentViewController* parentViewController;

// More items

@end

Displaying ControllerToBeShownModally:

ControllerToBeShownModally *ctrl = [[ControllerToBeShownModally alloc] init...];
ctrl.parentViewController = self; // Store the back reference here
[self presentModalViewController:ctrl animated:YES];

Calling back:

[self.parentViewController doWhatIWant];

Upvotes: 3

Related Questions