如何从同一个故事板访问 Objective-C 和 Swift 类?

How to access both Objective-C and Swift classes from same storyboard?(如何从同一个故事板访问 Objective-C 和 Swift 类?)

本文介绍了如何从同一个故事板访问 Objective-C 和 Swift 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Swift 中实现了一个 UIButton 派生类(RoundButton),其余的仍在 Objective-C 中.如何在同一个 Storyboard 中同时访问基于 Objective-C 的类和基于 Swift 的类?

I have implemented one UIButton-derived class (RoundButton) in Swift and the rest is still in Objective-C. How do I access both Objective-C-based classes and Swift-based classes in the same storyboard?

您可以在 Storyboard 的 UIButton Custom Class - Class 字段中键入 RoundButton,但 iOS 会给出以下警告运行时:

You can type RoundButton in a UIButton Custom Class - Class field in a Storyboard, but iOS gives the following warning runtime:

Unknown class RoundButton in Interface Builder file.

推荐答案

看来Xcode 6.0 (6A215l) 需要将Storyboard 文件升级到更新版本才能访问Swift 类.

It seems that Xcode 6.0 (6A215l) needs to upgrade the Storyboard file to a newer version before it can access Swift classes.

如果您将 Objective-C 类替换为同名的 Swift 类,则需要打开 Storyboard,编辑自定义类的名称(如删除最后一个字母),保存 Storyboard,然后重命名该类背部.这将强制 Storyboard 文件升级到更新的版本,并且新的 Xcode 正确地编写自定义类键,如下所示:

If you replaced an Objective-C class with a Swift class with the same name, you need to open the Storyboard, edit the name of the custom class (like remove the last letter), save the Storyboard and then rename the class back. This will force the Storyboard file to be upgraded to a newer version, and the new Xcode to correctly write the custom class key like this:

版本信息差异:

UIButton 自定义类定义差异:

UIButton custom class definition diff:

在 Storyboard 文件中进行这些更改后,警告似乎消失了,并且使用了正确的自定义类.

After these changes in the Storyboard file the warning seems to disappear and the correct custom class is used.

这篇关于如何从同一个故事板访问 Objective-C 和 Swift 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何从同一个故事板访问 Objective-C 和 Swift 类?

基础教程推荐