【问题标题】:Can a UI class with different Obj-C and Swift names be used in Storyboard?可以在 Storyboard 中使用具有不同 Obj-C 和 Swift 名称的 UI 类吗?
【发布时间】:2016-08-11 00:00:06
【问题描述】:

我有一个 UIButton Swift 子类 CustomButton,我想向 Obj-C 公开一个前缀名称。

@objc(PrefixCustomButton) public class CustomButton: UIButton { ... }

此类存在于 Swift 模块 CustomModule 中。我正在通过 Cocoapods 将此模块集成到 Objective-C 和 Swift 应用程序中以测试 CustomButton。当我尝试使用 Storyboard 中的 CustomButton 时,出现以下错误:

Unknown class _TtC9CustomModule17CustomButton in Interface Builder file.

我也尝试在 Storyboard 中使用 PrefixCustomButton 并得到同样的错误:

Unknown class _TtC9CustomModule22PrefixCustomButton in Interface Builder file.

编辑:@matt 是正确的,PrefixCustomButton 必须在 Storyboard 中使用(对于 Swift 和 Obj-C 项目)。我缺少的附加部分是我不需要设置模块名称。

【问题讨论】:

  • @objc(PrefixCustomButton) CustomButton: UIButton { ... } 这不是 Swift。也许你的意思是@objc(PrefixCustomButton) class CustomButton: UIButton { ... }
  • 是的,抱歉,将编辑!

标签: ios objective-c swift uibutton storyboard


【解决方案1】:

您自己说过:您向 Objective-C 公开了一个不同的名称。因此,就 Interface Builder 而言,该类的名称是 PrefixCustomButton。

【讨论】:

  • 如果我使用 PrefixCustomButton,我会得到“Unknown class _TtC9AppName22PrefixCustomButton in Interface Builder file.”
  • 好吧,我不知道。一定是发生了其他事情。
  • 如果我将按钮添加为 Storyboard 中的 UIView 而不是 UIButton,这似乎可行。
  • 这是我的 UIButton。如果它可以用作 UIView,我会感到惊讶;您不能仅通过将 UIView 转换为按钮将其转换为工作按钮。 — 也许你的项目被水洗了。我将从清除缓存等开始。您获得类的名称这一事实也很不寻常。当我调用我的 CustomButton 时,我的错误仍然给出了正确的名称,“CustomButton”,作为麻烦类的名称。
  • 我在一个 Swift 模块中有CustomButton,我通过 Cocoapods 将它集成到 Swift 和 Objective-C 项目中,我试图将它添加到 Storyboard 中。到目前为止,我看到的是在 Storyboard 中添加一个 UIButton 组件并将自定义类设置为PrefixCustomButton + 设置模块名称,我得到了上述错误。当我添加 UIView 组件并将自定义类设置为 PrefixCustomButton 并且不设置模块名称时,它可以工作。
猜你喜欢
  • 2011-10-16
  • 1970-01-01
  • 1970-01-01
  • 2013-05-16
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多