What is the proper way to dismiss a modal when usi

2020-05-24 19:19发布

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 parent view controller after a button click?

6条回答
【Aperson】
2楼-- · 2020-05-24 19:48

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

查看更多
手持菜刀,她持情操
3楼-- · 2020-05-24 19:51

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

查看更多
Viruses.
4楼-- · 2020-05-24 19:57

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

查看更多
祖国的老花朵
5楼-- · 2020-05-24 19:58

To do this inside the UIStoryboard you need first to create an Object of the type UIStoryboardSegue in your project

Creating a new class of type <code>UIStoryboardSegue</code>

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.

enter image description here

Select it and you are done! There is also a very good explanation on this website.

查看更多
三岁会撩人
6楼-- · 2020-05-24 20:04

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.

查看更多
在下西门庆
7楼-- · 2020-05-24 20:09

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

查看更多
登录 后发表回答