使用故事板时关闭模式的正确方法是什么?
使用故事板,关闭模式的正确方法是什么?
- 使用 IBAction 并编写代码在单击按钮后关闭?
- 使用 segue 并在单击按钮后通知父视图控制器?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请参阅此处关闭呈现的视图控制器大约一半
因此,您应该使用 IBAction 并编写代码以在单击按钮后关闭
See Here Dismissing a Presented View Controller about halfway down
So you should use an IBAction and writing code to dismiss after a button click
根据 Alex Cio 对 Swift 3 和 XCode 8.3 的回答:
创建类:
但在故事板中你应该选择:
仅当此选项出现在
Action Segue
菜单上后才According Alex Cio answer for
Swift 3
andXCode 8.3
:Create class:
But in storyboard you should choose:
Only after this option appear on
Action Segue
menu我发现通常当我尝试在故事板中执行此操作时,我宁愿不创建额外的类。从呈现视图控制器执行关闭仍然有意义,因此需要一个类来支持它。
如果您在呈现视图控制器中创建一个 IBAction 并对其进行适当的命名,例如
然后从故事板中您想要触发解雇的任何位置,您将创建一个到第一响应者的操作,如下所示。您可以通过为 IBActions 创建唯一的名称来扩展它以与多个呈现视图控制器一起使用。
有关第一响应者和响应者链的更多信息
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.
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
请参阅我的回答此处。它为您提供了两种使用故事板关闭模态视图控制器的方法。我喜欢所描述的方法二,因为您在项目中添加类,您可以从模式视图返回,而无需单独使用故事板来完成代码。也就是说,如果您已经实现了委托和委托协议,那么它也是放置 dismissModalViewController 语句的好地方。
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.
要在
UIStoryboard
中执行此操作,您首先需要在项目中创建UIStoryboardSegue
类型的对象然后在类中插入以下方法。这是我的类
@implementation DismissController
现在您可以在
UIStoryboard
中使用它。选择应该使UIViewController
消失的按钮,并将其拖动到您想要转到的UIViewController
上。就我而言,它显示 **dismiss Controller* 因为我的班级名称。选择它,你就完成了!
关于这一点还有一个很好的解释网站。
To do this inside the
UIStoryboard
you need first to create an Object of the typeUIStoryboardSegue
in your projectThen insert following method inside the class. Here is my class
@implementation DismissController
Now you can use it inside your
UIStoryboard
. Select the button that should make theUIViewController
Disappear and drag it to theUIViewController
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.
正如 Apple 在线文档所示,呈现视图控制器负责关闭模态(呈现)视图。
有一个帖子和示例可用
这里
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