If you are using a Storyboard segue to present your view controller you could dismiss it using an Unwind Segue. An unwind segue is a special kind of segue that unwinds the presented view controllers back to a presenter.
To accomplish this, you would create a method in the presenting view controller with the following signature:
- (IBAction)unwindAction:(UIStoryboardSegue*)unwindSegue;
This is different than a standard IBAction
because the parameter type is a UIStoryboardSegue*
instead of the normal id
type (it doesn't have to be named unwindSegue:
, it could be modalViewFinished:
or whatever you like - the important part is that it has a return type of IBAction
and a parameter type of UIStoryboardSegue*
).
Once you have this method defined, in your storyboard you control-drag from the modal view controller icon (below its view, in the little bar of icons) and release the connection on the green exit sign. This will create an unwind segue, which you should give an identifier
in the attributes inspector. Unwind segues will not show up visually in the storyboard canvas, so you will have to find it in the list of items on the left side of the canvas (this is collapsed by default, expand it by clicking the little circular button in the lower left hand corner of the canvas).
Once you've done that, rather than calling [self dismissViewControllerAnimated:YES completion:nil]
, just call [self performSegue:<Identifier you gave the unwind segue>]
instead. During this process the unwindAction:
method defined on the presenting view controller and the prepareForSegue:
method on the modal view controller should be invoked. You can do whatever cleanup you need to do in these methods (calling the foo
method from unwindSegue:
, for example).