Using storyboards, what is the proper way to dismiss a modal?
To do this inside the UIStoryboard
you need first to create an Object of the type UIStoryboardSegue
in your project
Then insert following method inside the class. Here is my class
@implementation DismissController
- (void)perform{
UIViewController *sourceVC = self.sourceViewController;
[sourceVC.presentingViewController dismissViewControllerAnimated:YES
completion:nil];
}
Now you can use it inside your UIStoryboard
. Select the button that should make the UIViewController
Disappear and drag it to the UIViewController
you want to go to. In my case it shows **dismiss Controller* because of the name of my Class.
Select it and you are done! There is also a very good explanation on this website.
According Alex Cio answer for Swift 3
and XCode 8.3
:
Create class:
import UIKit
class DismissSegue: UIStoryboardSegue {
override func perform() {
self.source.presentingViewController?.dismiss(animated: true, completion: nil)
}
}
But in storyboard you should choose:
Action Segue -> Custom -> dismiss
Only after this option appear on Action Segue
menu
As the Apple online documentation indicates, the presenting view controller is responsible for dismissing the modal (presented) view.
There's a post and example available here
I've found that usually when I'm attempting to do this in storyboard I'd rather not create extra classes. It still makes sense to perform the dismiss from the presenting view controller, so that requires a class to back it.
If you create an IBAction in the presenting view controller and name it appropriately e.g.
- (IBAction)dismissAnyModel:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Then from storyboard wherever you want to trigger the dismiss from you create an action to the first responder as shown below. You can extend this to work with multiple presenting view controllers by creating unique names for the IBActions.
More information on first responder and the responder chain
See my answer here. It gives you two ways to dismiss the modal view controller with storyboard. I like method two described because one you add the class in your project your return from modal views can be done with no code using storyboard alone. That said, if you have implemented a delegate and delegate protocol, it is also a good place to put the dismissModalViewController statement.
See Here Dismissing a Presented View Controller about halfway down
When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it.
So you should use an IBAction and writing code to dismiss after a button click