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

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

问题描述

我在Swift中实现了一个UIButton派生类(RoundButton),其余的仍然在Objective-C中。如何在同一个故事板中访问基于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?

您可以键入 RoundButton 在故事板中的UIButton 自定义类 - 类字段中,但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.

如果使用同名的Swift类替换Objective-C类,则需要打开故事板,编辑自定义类的名称(如删除最后一个字母),保存故事板,然后重命名该类。这将强制将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:

版本信息diff:

Version information diff:

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类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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