What is the proper way to dismiss a modal when using storyboards? What is the proper way to dismiss a modal when using storyboards? ios ios

What is the proper way to dismiss a modal when using storyboards?


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


According Alex Cio answer for Swift 3 and XCode 8.3:

Create class:

import UIKitclass 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


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.

Create an outlet to first responder

Select correct IBAction

More information on first responder and the responder chain