无法在 Xcode 中连接 IBAction

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28732903/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:43:44  来源:igfitidea点击:

Can't connect IBAction in Xcode

iosxcodeswift

提问by fedoroffn

When I drag from a button on the storyboard to my view controller Swift file, I only get the option to insert an Outlet or Outlet Collection.

当我从故事板上的按钮拖动到我的视图控制器 Swift 文件时,我只能选择插入插座或插座集合。

Similarly, if I create the IBAction manually and try to connect it to a button on the storyboard, I can't connect the two.

同样,如果我手动创建 IBAction 并尝试将其连接到故事板上的按钮,则无法将两者连接起来。

This problem is only appears in one of my projects, but it happens on all the storyboards in my project.

这个问题只出现在我的一个项目中,但它发生在我项目的所有故事板上。

Seems like an Xcode bug, but maybe someone knows of a workaround.

似乎是 Xcode 错误,但也许有人知道解决方法。

(It's hard to tell, but the element being dragged in the screenshot is a button.)

(很难说,但截图中被拖动的元素是一个按钮。)

enter image description here

在此处输入图片说明

回答by manicaesar

I have very similar issue in my Swift project that was driving me mad.

我在我的 Swift 项目中遇到了非常相似的问题,这让我很生气。

It turned that's it is the problem of mixing custom protocol adoption in extension of UIButtonhaving @IBInspectableproperty. More details below.

原来,这是在UIButton拥有@IBInspectable属性的扩展中混合自定义协议采用的问题。更多详情如下。

I finally found out that's because I have following code in my project: (copyright goes to Kevin McNeigh: https://www.iphonelife.com/blog/31369/swift-programming-101-mastering-dynamic-type-ios)

我终于发现这是因为我的项目中有以下代码:(版权归凯文麦克尼所有:https://www.iphonelife.com/blog/31369/swift-programming-101-mastering-dynamic-type-ios )

protocol DynamicTypeChangeHandler {

    var typeObserver: Bool {get set}
}

extension UIButton: DynamicTypeChangeHandler {
    @IBInspectable var typeObserver: Bool {
        // ...
    }
}

If I remove either protocol adoption from the extension or @IBInspectable keyword, I can again setup actions in Interface Builder. Sheer lunacy. Xcode <3

如果我从扩展中删除协议采用或 @IBInspectable 关键字,我可以再次在 Interface Builder 中设置操作。纯粹的疯子。Xcode <3

回答by Lin

You can try to change the @IBAction's argument from (_ sender: Any)to (_ sender: UIButton)or (_ sender: AnyObject), but manually.

您可以尝试将@IBAction的参数从更改(_ sender: Any)(_ sender: UIButton)(_ sender: AnyObject),但要手动更改。

It might work.

它可能会起作用。

回答by kellanburket

I can't speak to the underlying cause--something always seems to be breaking in XCode, but you can always write your IBAction manually, @IBAction func(sender: UIButton) { }in your view/controller and then control-drag from the storyboard element to the action itself. If that doesn't work try linking from the node created by an IBAction in the margin of your code back to the storyboard. This works for me when XCode stops working.

我无法说出根本原因——在 XCode 中似乎总是有一些问题,但你总是可以手动编写你的 IBAction,@IBAction func(sender: UIButton) { }在你的视图/控制器中,然后从故事板元素控制拖动到动作本身。如果这不起作用,请尝试从代码边缘的 IBAction 创建的节点链接回故事板。当 XCode 停止工作时,这对我有用。

回答by Mohammad Nurdin

Make sure you select Automatic.

确保选择Automatic.

enter image description here

在此处输入图片说明

回答by Beny Boariu

This bug is indeed super crazy. I lost hours because of it. Until now I found 2 reasons, and I'll post here maybe somebody will read and be helpful:

这个bug确实超级疯狂。我因此损失了几个小时。到目前为止,我发现了 2 个原因,我会在这里发帖,也许有人会阅读并提供帮助:

  1. If anybody is using BonMotpod, this is one possible cause. If you remove it, the IBActionwill be back.

  2. Any extension on UIButtonor UIBarButtonItemcan cause this as well. Comment the extension, and the IBActionwill be back. Happy coding.

  1. 如果有人在使用BonMotpod,这是一个可能的原因。如果您删除它,IBAction将返回。

  2. UIButtonUIBarButtonItem上的任何扩展也可能导致这种情况。注释扩展名,IBAction就会回来。快乐编码。



UPDATE: BonMotfixed this issue in 4.0.2

更新:BonMot4.0.2 中修复了这个问题

回答by Will Stevens

This apparently is a known issue on the Apple forums, but I don't know if Apple is working on it:

这显然是 Apple 论坛上的一个已知问题,但我不知道 Apple 是否正在处理它:

https://forums.developer.apple.com/message/216258

https://forums.developer.apple.com/message/216258

Here is what I did for a workaround.

这是我为解决方法所做的工作。

  1. Go into my Podfile and comment out the pod library (in my case, ReactiveKit Bond) that is doing an extension causing the issue.
  2. Do a pod updateto remove the pod from the project.
  3. Go back in and do my IBAction connections in IB like I normally would. Do as many together as possible for efficiency.
  4. Finally go back and uncomment your pod library and do another pod updateto reimport the pods.
  5. Test by setting a breakpoint in your new IBAction functions to verify your connections are working.
  1. 进入我的 Podfile 并注释掉正在执行导致问题的扩展的 pod 库(在我的例子中是 ReactiveKit Bond)。
  2. 执行 apod update从项目中删除 pod。
  3. 像往常一样返回并在 IB 中执行我的 IBAction 连接。尽可能多地一起做以提高效率。
  4. 最后返回并取消注释您的 pod 库并执行另一个操作pod update以重新导入 pod。
  5. 通过在新的 IBAction 函数中设置断点进行测试,以验证您的连接是否正常工作。

回答by toddg

For me, I was trying to connect multiple buttons to one IBAction, none of the solutions worked until I switched my method signature to specify that the sender was a UIButtonrather than Any

对我来说,我试图将多个按钮连接到一个IBAction,直到我切换我的方法签名以指定发件人是一个UIButton而不是Any

@IBAction func buttonPressed(_ sender: UIButton) {
}

回答by user3836066

If extension is causing the problem, then instead of directly extending UIButton, user typealias e.g, create extension of CustomButton instead of UIButton

如果扩展导致了问题,那么不是直接扩展 UIButton,而是用户类型别名,例如,创建 CustomButton 的扩展而不是 UIButton

typealias CustomButton = UIButton
extension CustomButton { }

回答by Smiles4U

I was having the same issue but it was because I was trying to connect an ImageView to an IBAction (forgive me, not sure if my terminology is correct). I deleted the ImageView object and replaced it with a Button and IBAction then became an option rather than just IBOutlet and Outlet Collection.

我遇到了同样的问题,但这是因为我试图将 ImageView 连接到 IBAction(请原谅我,不确定我的术语是否正确)。我删除了 ImageView 对象并用一个 Button 和 IBAction 替换它然后成为一个选项而不仅仅是 IBOutlet 和 Outlet Collection。

回答by vsenn

For me, it was a FUIButton (custom from SAP Fiori) causing same issue. No pods or UIButton extension specified in my own code (in addition to SAP UIButton obviously). Cmd + Alt + Shift + K, DerivedData Clean didn't help either. @IBAction wasn't available in Interface Builder till I changed my Button Class back to simple UIButton.

对我来说,这是一个 FUIButton(来自 SAP Fiori 的自定义)导致了同样的问题。我自己的代码中没有指定 Pod 或 UIButton 扩展(显然除了 SAP UIButton)。Cmd + Alt + Shift + K,DerivedData Clean 也没有帮助。@IBAction 在界面生成器中不可用,直到我将按钮类改回简单的 UIButton。

And... voila! ta-da!

还有……瞧!达达!