本文介绍了从Modal View Controller调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一个视图控制器,当点击某个按钮时会显示一个模式视图.关闭模态视图并在下面重新显示原始视图后,我希望调用refresh方法.如何在ModalViewController中的OriginalViewController中调用此refresh:方法?

I have a view controller which presents a modal view when a certain button is tapped. Upon closing the modal view and re-revealing the original view underneath, I want a refresh method to be called. How do I call this refresh: method in OriginalViewController from ModalViewController?

如果在-viewDidAppear中执行此操作,我知道这是可行的,但我只希望它在模式视图关闭时发生,而不是每次都关闭.

I know this works if I do it in -viewDidAppear, but I only want it to happen when the modal view closes, not every single time.

推荐答案

您可以在《视图控制器编程指南》 ,建议的方法是使用委托.

As you can see in the View Controller Programming Guide, the recommended way is to use delegation.

您如何操作取决于您,但是一种标准方法是定义一个协议,例如:

How do you do it is up to you, but a standard way to so would be to define a protocol such as:

@protocol RecipeAddDelegate <NSObject>
- (void)modalViewControllerDismissed:(ModalViewController *)modalViewController;
@end

然后在您的OriginalViewController上可以实现该方法,并在关闭模态视图控制器后执行以下操作:

Then on your OriginalViewController you can implement that method, and act when the modal view controller has been dismissed:

- (void)modalViewControllerDismissed:(ModalViewController *)modalViewController {
   [self refresh]; // or anything you want to do
}

作为补充说明,我链接的指南建议您不要从模态本身而是从打开它的控制器中退出模态.在该示例中,他们创建的委托协议有些不同,因此它具有使原始控制器了解模态控制器所执行操作的方法,并能够决定何时关闭它.

As an additional comment, the guide I linked suggest that you should dismiss the modal not from the modal itself but from the controller that opened it. In the example, they create the delegate protocol a bit different, so it has methods for the original controller to be informed of the actions the modal controller does, and be able to decide when to close it.

这篇关于从Modal View Controller调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:41