Call method from Modal View Controller

戏子无情 提交于 2019-12-11 06:54:59

问题


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?

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.


回答1:


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

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.




回答2:


Have a look at the View Controller Programming Guide, specifically, the section on dismissing a modal view.

The OriginalViewController should have a protocol method called by the ModalViewController when it's done. It should be the OriginalViewControllers responsibility to dismiss the modal view and perform any tasks it needs to on itself, such as refreshing itself.



来源:https://stackoverflow.com/questions/1675866/call-method-from-modal-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!