Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 646
Jack Avatar asked Feb 03 '10 16:02

Jack


People also ask

How do you dismiss a presented view controller in Objective C?

When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it.

How do I dismiss a two view controller in Swift?

[self. presentingViewController. presentingViewController dismissViewControllerAnimated:YES completion:nil];

How do you dismiss a screen in Swift?

HomeScreen will have a button called "Present", which will present another screen called PromotionsScreen . A PromotionsScreen will have one more button to dismiss the presented screen and go back to the HomeScreen.


2 Answers

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

like image 56
Grouchal Avatar answered Oct 15 '22 02:10

Grouchal


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.

like image 32
Jasarien Avatar answered Oct 15 '22 04:10

Jasarien