开发者

Call method from Modal View Controller

开发者 https://www.devze.com 2022-12-11 01:22 出处:网络
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. H

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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消