使用故事板时关闭模式的正确方法是什么?

发布于 2025-01-07 13:02:51 字数 109 浏览 1 评论 0原文

使用故事板,关闭模式的正确方法是什么?

  • 使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

是伱的 2025-01-14 13:02:51

请参阅此处关闭呈现的视图控制器大约一半

当需要关闭呈现的视图控制器时,首选方法是让呈现的视图控制器关闭它。

因此,您应该使用 IBAction 并编写代码以在单击按钮后关闭

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

一张白纸 2025-01-14 13:02:51

根据 Alex Cio 对 Swift 3 和 XCode 8.3 的回答:

创建类:

import UIKit

class DismissSegue: UIStoryboardSegue {
    override func perform() {
        self.source.presentingViewController?.dismiss(animated: true, completion: nil)
   }
}

但在故事板中你应该选择:

动作继续 ->定制->解雇

仅当此选项出现在 Action Segue 菜单上后才

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

最舍不得你 2025-01-14 13:02:51

我发现通常当我尝试在故事板中执行此操作时,我宁愿不创建额外的类。从呈现视图控制器执行关闭仍然有意义,因此需要一个类来支持它。

如果您在呈现视图控制器中创建一个 IBAction 并对其进行适当的命名,例如

- (IBAction)dismissAnyModel:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

然后从故事板中您想要触发解雇的任何位置,您将创建一个到第一响应者的操作,如下所示。您可以通过为 IBActions 创建唯一的名称来扩展它以与多个呈现视图控制器一起使用。

创建第一响应者的出口

选择正确的 IBAction

有关第一响应者和响应者链的更多信息

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

梦中楼上月下 2025-01-14 13:02:51

请参阅我的回答此处。它为您提供了两种使用故事板关闭模态视图控制器的方法。我喜欢所描述的方法二,因为您在项目中添加类,您可以从模式视图返回,而无需单独使用故事板来完成代码。也就是说,如果您已经实现了委托和委托协议,那么它也是放置 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.

遗失的美好 2025-01-14 13:02:51

要在 UIStoryboard 中执行此操作,您首先需要在项目中创建 UIStoryboardSegue 类型的对象

创建一个新的类型类<code>UIStoryboardSegue</code>

然后在类中插入以下方法。这是我的类

@implementation DismissController

- (void)perform{

    UIViewController *sourceVC = self.sourceViewController;
    [sourceVC.presentingViewController dismissViewControllerAnimated:YES 
                                                          completion:nil]; 
}

现在您可以在 UIStoryboard 中使用它。选择应该使 UIViewController 消失的按钮,并将其拖动到您想要转到的 UIViewController 上。就我而言,它显示 **dismiss Controller* 因为我的班级名称。

输入图像描述在这里

选择它,你就完成了!
关于这一点还有一个很好的解释网站。

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.

一江春梦 2025-01-14 13:02:51

正如 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文