16

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

2 Answers 2

24

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.

It is really simple to do and think more elegant that than using viewWillDisappear - as there are other reasons why view could dissapear!

create a protocol on your modal ViewController - xViewControllerDelegate

@protocol xViewControllerDelegate

    - (void) modalDialogFinished;

@end

Then make your parent implement the delegate using the <xViewControllerDelegate> when you define your parent view controller.

You will be forced to have a method called modalDialogFinished in your parent view controller - which can handle dismiss command and the refresh etc.

Remember to pass anid<xViewControllerDelegate> into the modal view controller in your init code and store it as a field on the object.

When you want to disssmiss your modal view then you just need to reference the 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:

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

1
  • This should be the top rate answer Jul 9, 2012 at 2:28
20

UIViewController has a property called 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.

In your modal view controller, in viewWillDisappear: you can send a message to the parentViewController to perform any action you wish, essentially.

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]; to do what you're trying to achieve.

8
  • Thanks a lot for the quick reply Jasarien, will give that a go a bit later.
    – Jack
    Feb 3, 2010 at 16:52
  • I tried this and I got this error. Am I doing it wrong? I added a method to the parent view called it "reloadData" then called this in viewWillDisappear: [self.parentViewController reloadData]; -[UITabBarController reloadData]: unrecognized selector sent to instance 0x4a215e0'
    – Brodie
    May 11, 2010 at 12:21
  • 1
    Be careful: viewWillDisappear does not mean that the modal was dismissed, it means it is no longer what the user is seeing. It will also fire if the modal is covered up by, for instance, a second modal. When that second modal is dismissed, the original modal is still there and will get a second viewDidAppear call, etc. I have been searching for a signal that really means the modal was actually dismissed, and I can't find one. Jul 23, 2012 at 22:08
  • 1
    Doesn't this break good design, as now the parent knows the child, and the child knows the parent (the latter should be avoided)? @Grouchal's answer seems more confining Dec 11, 2013 at 14:23
  • 1
    The child view controller shouldn't know anything about the parent, but the other way round! The correct way to do this, would be to use delegates.
    – Jochen
    Mar 31, 2015 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.