tags:

views:

514

answers:

2

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.

+2  A: 

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.

Ben S
+2  A: 

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.

pgb
Link broken, new link is: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
Kalle
Thank you @Kalle, I just updated the answer.
pgb
What is "RecipeAddDelegate"? Where does "OriginalViewController" and "ModalViewController" go? Who has example code that actually *ANSWERS* the original question. Actual code.
Patricia