ios 使用故事板时关闭模态的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9382099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the proper way to dismiss a modal when using storyboards?
提问by aryaxt
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?
- 使用 IBAction 并编写代码在单击按钮后关闭?
- 使用 segue 并在单击按钮后通知父视图控制器?
采纳答案by Bill Bonar
See Here Dismissing a Presented View Controllerabout 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
因此,您应该使用 IBAction 并编写代码以在单击按钮后解除
回答by UnRewa
According Alex Cio answer for Swift 3
and XCode 8.3
:
根据 Alex Cio 对Swift 3
和 的回答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
只有在此选项出现在Action Segue
菜单上之后
回答by Chris Mitchelmore
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 并适当地命名它,例如
- (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.
然后从情节提要中您想要触发解除的任何位置创建一个对第一响应者的操作,如下所示。您可以通过为 IBActions 创建唯一名称来扩展它以使用多个呈现视图控制器。
回答by 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.
在这里看到我的答案。它为您提供了两种使用故事板关闭模态视图控制器的方法。我喜欢描述的方法二,因为您在项目中添加了类,您可以仅使用故事板在没有代码的情况下从模态视图返回。也就是说,如果你已经实现了一个委托和委托协议,那么它也是放置dismissModalViewController 语句的好地方。
回答by Alex Cio
To do this inside the UIStoryboard
you need first to create an Object of the type UIStoryboardSegue
in your project
要在其中执行此操作,UIStoryboard
您首先需要UIStoryboardSegue
在项目中创建该类型的对象
Then insert following method inside the class. Here is my class
然后插入以下的类中的方法。这是我的课
@implementation DismissController
@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.
现在你可以在你的UIStoryboard
. 选择应该使UIViewController
消失的按钮并将其拖动到UIViewController
您想要去的位置。在我的情况下,它显示 **dismiss Controller* 因为我的班级的名称。
Select it and you are done! There is also a very good explanation on this website.