简体   繁体   中英

IOS How to call a method in a ViewController if modal is dismissed

In all view controllers I present a ModalViewController after I dismiss modal view controller I want to call a method in current view controller.

I have to presentModalViewController I cant push it because it is a form sheet. Since I cant push it (void)viewDidAppear:(BOOL)animated is not called when I dismiss the form sheet.

Btw form sheet is a settings menu and I have to call it in every view controller, so I cant use notifications because there are over 20 View controllers and only one settings menu;

Navigation controller -> Root- > VC1 - > VC2 - > VC3 ->VC4........... VC20......
                                  |       |       |     |               |
                                 Menu    Menu    Menu  Menu           Menu

I present menu:

UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                              bundle:nil];
SettingsListViewController *settingsVC = [sb instantiateViewControllerWithIdentifier:@"SettingsListViewController"];
UINavigationController *modalViewNavController= [[UINavigationController alloc] initWithRootViewController:settingsVC];
modalViewNavController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
modalViewNavController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:modalViewNavController animated:YES];

I dismiss it :

 //dissmiss view
 [self.navigationController dismissModalViewControllerAnimated:YES] ;

In View Controllers I want to Call; [self.navigationController popToRootViewControllerAnimated:NO];

Is there a way to call a method in View Controller when form sheet is dismissed ?

Since iOS 5, you can use the presentingViewController property of every UIViewController to see 1) if they're being presented modally in the first place and 2) who it is that's presenting them modally then. So if you present your form sheet by calling [self.navigationController presentModalViewController:modalViewNavController animated:YES] , the presenting view controller will then be the root navigation controller and you can tell it to pop to root at the same time you dismiss the modal presentation.

By the way, there's also a storyboard property in every view controller which has originated from a storyboard, so you could use that one directly when instantiating new storyboard view controllers by name.

自己创建一个委托,并设置将视图显示为委托的视图控制器。在要关闭该视图时从modalVC调用。

You can set modalViewController.parentViewController = self; and then work with it from modal view controller like that if you want to send messages before dismiss:

- (void)viewWillDisappear:(BOOL)animated {
    [self.parentViewController doSomething];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM