[英]How to use XIB in a Swift project iOS
I had two separate Xcode projects that I have fused into a single one: a speech to text recognizer that is implemented with a XIB file, written in Objective-C, and another project that is just a conventional UI written in Swift with some more stuff. 我有两个独立的Xcode项目,我已融合成一个:一个用文本识别器实现的文档识别器,用Objective-C编写的XIB文件,另一个项目只是一个用Swift编写的传统用户界面,有更多的东西。 Both projects work flawlessly by themselves.
这两个项目都是完美无缺的。 I have cleaned out all the errors, and created a Bridging header file.
我清除了所有错误,并创建了一个Bridging头文件。 So far, both Swift and Objective-C modules coexist, but obviously only the Swift part runs.
到目前为止,Swift和Objective-C模块共存,但显然只有Swift部分运行。 I want to open the Objective-C XIB and display it in my swift-coded UI.
我想打开Objective-C XIB并在我的swift编码UI中显示它。
According to your screenshot, I assume you are using storyboard for your UI in your Swift application. 根据您的屏幕截图,我假设您在Swift应用程序中使用了故事板。
As your legacy UI is on XIB files, you need to load it programatically. 由于旧版UI位于XIB文件上,因此需要以编程方式加载它。 To do that, you'll have to use
loadNibNamed
method like in the following example : 为此,您必须使用
loadNibNamed
方法,如以下示例所示:
var nibs = NSBundle.mainBundle().loadNibNamed("Counter", owner: self, options: nil)
for v in nibs {
if (v is Counter) {
someViewInStoryboard.addSubview(v)
}
}
Here I have a Counter.xib file which contains an custom UIView
. 这里我有一个Counter.xib文件,其中包含一个自定义的
UIView
。 Class for this view is Counter
, which extends UIView
. 此视图的类是
Counter
,它扩展了UIView
。
So when your XIB is loaded, browse it to find your custom view, then add it as a subview of an existing UIView
in your storyboard. 因此,当您加载XIB时,浏览它以查找自定义视图,然后将其添加为故事板中现有
UIView
的子视图。 Variable someViewInStoryboard
in my example is an IBOutlet
of this UIView
. 在我的例子中,变量
someViewInStoryboard
是这个UIView
的IBOutlet
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.