为什么 UIBarButtonItem 在 segue 之前不调用 IBAction? [英] Why UIBarButtonItem doesn't call IBAction before segue?

查看:28
本文介绍了为什么 UIBarButtonItem 在 segue 之前不调用 IBAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题非常类似于:Swift: Make button trigger segue到新场景

问题是:

我有一个视图控制器,带有一个按钮,可以模态显示另一个视图控制器.

I have a view controller, with a button that causes another view controller to appear modally.

我有 Ctrl+Click 从按钮到第二个视图控制器,并在 IB 中创建了 segue.

I have Ctrl+Click from the button to the second View Controller, and created the segue in IB.

然后我Ctrl+Click再次从按钮到视图控制器的源代码来创建一个IBAction方法.

Then I Ctrl+Click from the button again to the source code of the view controller to create an IBAction method.

我假设按钮现在会做两件事:a) 调用 IBAction 方法,以及 b) 执行转场.

I assumed that the button will do two things now: a) call the IBAction method , and b) perform the segue.

发生的事情只是出于某种原因的转场.

What happens is only the segue for some reason.

当我删除 segue 或从 IB 中删除对视图控制器的调用时,会调用 IBAction,但 Xcode 告诉我现在无法访问第二个视图控制器.

When I delete the segue, or remove the call to the view controller from IB, then the IBAction is called, but Xcode tells me that the second view controller is not reachable now.

我希望能够向用户呈现一个 ActionSheet,然后能够根据用户从操作表中选择的内容向第二个视图控制器执行转场.

I want to be able to present an ActionSheet to the user and then be able to performSegue to the second view controller, based on what the user selected from the action sheet.

我知道我可以以编程方式调用 performSegue 但这需要创建 segue 并将其附加到 IB 中的物理按钮,这违背了不调用按钮可能已经具有的 IBAction 的目的.

I know I can programatically call performSegue but that requires the creation of the segue and attaching it to a physical button in IB, which defeats the purpose of not calling the IBAction that button may already have.

推荐答案

如果我想在调用 segue 之前执行一些额外的步骤,我通常会将 IBAction 附加到 UIButton 并在代码中从中调用 perform segue.您可以向故事板添加转场并为其指定 ID,而无需将其连接到按钮.您可以通过按住 Control 单击视图控制器并拖动到情节提要中的下一个视图控制器来完成此操作.

If I want to do some additional steps before calling the segue I usually attach an IBAction to the UIButton and call the perform segue from within this, in the code. You can add a segue to the storyboard and give it an ID, without having to connect it to a button. You do this by control-clicking on the viewcontroller and drag to the next viewcontroller in the storyboard.

这篇关于为什么 UIBarButtonItem 在 segue 之前不调用 IBAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆