简体   繁体   English

模态视图被解除时的方法

[英]Method for when the modal view has been dismissed

I have created an application with a modal view that I can display and then dismiss. 我创建了一个带有模态视图的应用程序,我可以显示然后解散。 Is there an easy way to know when the modal view has been dismissed? 有没有一种简单的方法可以知道模态视图何时被解除? I would like to reload the data in a table once the modal view has been dismissed and don't know the best way of doing this. 一旦模态视图被解除,我想在表中重新加载数据,并且不知道这样做的最佳方式。

Thanks 谢谢

The recommended way to do this would be to use a delegate from your modal view controller back to the view controller that opened the view. 建议的方法是使用模态视图控制器中的委托回到打开视图的视图控制器。 Check out the official docs for examples. 查看官方文档以获取示例。

The reason that this is the recommended way is so that the ViewController that originally started the modal will also be in control of dismissing it. 推荐使用这种方法的原因是,最初启动模式的ViewController也可以控制解除它。

It is really simple to do and think more elegant that than using viewWillDisappear - as there are other reasons why view could dissapear! 与使用viewWillDisappear相比,这样做非常简单并且更优雅 - 因为还有其他原因导致视图消失!

create a protocol on your modal ViewController - xViewControllerDelegate 在模态ViewController上创建一个协议 - xViewControllerDelegate

@protocol xViewControllerDelegate

    - (void) modalDialogFinished;

@end

Then make your parent implement the delegate using the <xViewControllerDelegate> when you define your parent view controller. 然后在定义父视图控制器时使用<xViewControllerDelegate>使您的父实现委托。

You will be forced to have a method called modalDialogFinished in your parent view controller - which can handle dismiss command and the refresh etc. 你将被迫在父视图控制器中有一个名为modalDialogFinished的方法 - 它可以处理dismiss命令和刷新等。

Remember to pass an id<xViewControllerDelegate> into the modal view controller in your init code and store it as a field on the object. 请记住将id<xViewControllerDelegate>传递到初始化代码中的模态视图控制器,并将其作为字段存储在对象上。

When you want to disssmiss your modal view then you just need to reference the delegate.modalDialogFinished. 当你想要取消你的模态视图时,你只需要引用delegate.modalDialogFinished。

If this doesn't make sense then I can point you to some better example code - but I hope using delegates is not new to you. 如果这没有意义,那么我可以指出一些更好的示例代码 - 但我希望使用代表对您来说并不陌生。

UPDATE:: 更新::

Here is the official Apple documentation on how to do this for a modal view controller: 以下是关于如何为模态视图控制器执行此操作的Apple官方文档:

http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

UIViewController has a property called parentViewController . UIViewController有一个名为parentViewController的属性。 In the case that a view controller is presented modally, the parentViewController property points to the view controller that presented the modal view controller. 在以模态方式呈现视图控制器的情况下, parentViewController属性指向呈现模态视图控制器的视图控制器。

In your modal view controller, in viewWillDisappear: you can send a message to the parentViewController to perform any action you wish, essentially. 在模态视图控制器中,在viewWillDisappear:您可以向parentViewController发送消息,以执行您希望的任何操作。

Something like: 就像是:

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

If your parent view controller is a table view controller, then you should be able to call [self.parentViewController.tableView reloadData]; 如果您的父视图控制器是一个表视图控制器,那么您应该能够调用[self.parentViewController.tableView reloadData]; to do what you're trying to achieve. 做你想要实现的目标。

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

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