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

前端 未结 6 1052
终归单人心
终归单人心 2021-02-05 00:24

Using storyboards, what is the proper way to dismiss a modal?

  • using IBAction and writing code to dismiss after a button click?
  • using segue and notify the
相关标签:
6条回答
  • 2021-02-05 00:38

    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.

    0 讨论(0)
  • 2021-02-05 00:44

    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

    0 讨论(0)
  • 2021-02-05 00:44

    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

    0 讨论(0)
  • 2021-02-05 00:53

    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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-05 00:58

    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

    0 讨论(0)
提交回复
热议问题