How to execute code when view controller closed?

Hi


If you are setting up a viewcontroller in a segue as say a popover, how to you specify some closure or other code to be run when the popover is closed? Is there a standard way to do this? Googling has not helped. I want to refresh a table view after a popup may have changed the contents.


TIA
Mark

Replies

Scenario


Your tableView depends on some model data such as a sort order boolean, maybe a category list, the source list, etc.


Your tableView should update itself based on any of those model values changing independent of how they were changed. Maybe a change in a userPref, maybe a switch toggled. Your tableView controller would when know to update by monitoring these model values either explicitly by actions or indirectly by KVO.


Based on the above desired behavior, if you pass the model to be modified to your popover contained viewController via the segue, then there should be no need to "notify" the tableView on exit. The tableView already knows whenever the value of the model is changed. For example, if it is a small popover on a large screen such as the iPhone Plus, the user should see the changes live as they make them. Which means there is nothing left to do on exit of the popover. If you want the user to be able to change a bunch of values but only update the tableView when "done", then use a separate cloned model for recording the changes in the popover then transfer the changes to the real model during viewWillDisappear.

You have identified an issue with those iPad popovers. When a popover disappears (viewWillDisappear) it does not cause a message to the viewWillAppear method in the parent view which regains the full view and focus. In an iPhone that parent view's viewWillAppear method is messaged. So you need to implement something if there will be a difference in the underlying parent's view. You can do that most easily by adding, to the parentview, an observer for an NSNotification that updates your parent view and posting that notification in the popover either whenever it makes a change or when it will be dismissed. Actually, it's pretty cool to call it whenever a change in the parent is requested because you can 'see' the change being made underneath the popover.