ViewDidAppear not called when the modal view is dismissed
Asked Answered
M

2

8

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.

Melidamelilot answered 26/3, 2015 at 5:49 Comment(5)
Try using some other 'modalPresentationStyle'. Does it work then ?Crosswise
That is required by UX. I can't change it.Melidamelilot
Are you calling : [super viewDidAppear:animated];Crosswise
Yes, I called. I think the problem is that on iPhones, the modal view takes up the whole screen while on iPads, the modal view only is in the center.Melidamelilot
I tried reproducing your scenario, here is code. For me, its working on all device normally. Please, refer it. Hope it helps.Crosswise
T
7

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];
}
Twin answered 26/3, 2015 at 6:21 Comment(1)
what if the project uses storyboard+segues? I mean the situation when segue is defined in storyboard, not in codeOrontes
P
3

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).

Philoprogenitive answered 26/3, 2015 at 6:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.