简体   繁体   English

ios-如果关闭模态,如何在ViewController中调用方法

[英]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. 在所有视图控制器中,我关闭模态视图控制器后要present一个ModalViewController ,我想在当前视图控制器中调用方法。

I have to presentModalViewController I cant push it because it is a form sheet. 我必须要介绍presentModalViewController,因为它是一个表单,所以我不能推送它。 Since I cant push it (void)viewDidAppear:(BOOL)animated is not called when I dismiss the form sheet. 因为我不能推动它,所以我关闭表单时不会调用(void)viewDidAppear:(BOOL)animated

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; 顺便说一句,表单是一个设置菜单,我必须在每个View Controller中调用它,所以我不能使用通知,因为有20多个View Controller和一个设置菜单。

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 ? 关闭表单后,是否可以在View Controller中调用方法?

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. 从iOS 5开始,您可以使用每个UIViewControllerpresentingViewController属性来查看1)首先是否以模态形式呈现它们,以及2)然后以模态形式呈现它们。 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. 因此,如果您通过调用[self.navigationController presentModalViewController:modalViewNavController animated:YES]来展示[self.navigationController presentModalViewController:modalViewNavController animated:YES] ,则展示视图控制器将成为根导航控制器,并且您可以告诉它在[self.navigationController presentModalViewController:modalViewNavController animated:YES]模态展示的同时弹出到根目录。

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. 顺便说一句,每个视图控制器中都有一个storyboard属性,该属性源自故事板,因此在按名称实例化新的故事板视图控制器时,可以直接使用该属性。

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

You can set modalViewController.parentViewController = self; 您可以设置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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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