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

久未见 提交于 2019-12-03 02:02:55

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

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

UnRewa

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

T.J.

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!