[英]Class written in Swift cannot be a Nib Owner (Crash)
Here is what I am trying to do: Design a view in a nib file. 这是我要执行的操作:在nib文件中设计视图。 Back the view and nib owner with classes written in Swift.
用Swift编写的类支持视图和笔尖所有者。 Instantiate nib's view in Swift.
在Swift中实例化笔尖的视图。 Code that crash:
崩溃的代码:
var myViewOwner: MyViewOwner?
NSBundle.mainBundle().loadNibNamed("MyView", owner: myViewOwner, options: nil) // crash
... with error: ...错误:
[NSObject 0x7bfa0cc0 setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key myView.
[NSObject 0x7bfa0cc0 setValue:forUndefinedKey:]:此类与密钥myView的编码不兼容。
So, I have: nib file with blank view where view is of type MyClass
and nib owner of type MyViewOwner
. 所以,我有:带有空白视图的nib文件,其中视图的类型为
MyClass
而nib所有者为MyViewOwner
类型。 Both classes written in Swift. 这两个类都是用Swift编写的。
I will be posting only MyViewOwner class code since MyView.swift is just a class definition. 由于MyView.swift只是一个类定义,因此我将仅发布MyViewOwner类代码。
MyViewOwner.swift MyViewOwner.swift
class MyViewOwner {
@IBOutlet weak var myView: MyView
}
Caused error above. 导致上述错误。
Of course Swift objects are not KVO compliant, so I tried: 当然,Swift对象不符合KVO,所以我尝试了:
class MyViewOwner: NSObject {
@IBOutlet var myView: MyView
}
... and: ...和:
class MyViewOwner: NSObject {
@IBOutlet strong var myView: MyView = MyView() // we need to set something since it's 'strong'
}
Both caused error above. 两者都导致上面的错误。
@objc class MyViewOwner: NSObject {
@objc @IBOutlet strong var myView: MyView = MyView()
}
Didn't helped either, same error. 也没有帮助,同样的错误。
What I am doing wrong? 我做错了什么? In what way should I write all code in swift to make it work?
我应该以哪种方式迅速编写所有代码以使其正常工作?
UPDATE: Owner and View setup in xCode 更新: xCode中的所有者和视图设置
UPDATE: Owner to View connection 更新:所有者以查看连接
This code, if it is genuinely your code, makes no sense: 此代码(如果确实是您的代码)没有任何意义:
var myViewOwner: MyViewOwner?
NSBundle.mainBundle().loadNibNamed("MyView", owner: myViewOwner, options: nil)
The variable myViewOwner
in the first line is uninitialized; 第一行中的变量
myViewOwner
未初始化; there is no object there. 那里没有物体。 Hence that object cannot function as the
owner:
of anything, as you ask it to do in the second line. 因此,该对象不能充当
owner:
,你问它在第二行做任何事情。
Perhaps what you mean is: 也许您的意思是:
let myViewOwner = MyViewOwner() // or whatever the initializer is
NSBundle.mainBundle().loadNibNamed("MyView", owner: myViewOwner, options: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.