Here is what I am trying to do: Design a view in a nib file. Back the view and nib owner with classes written in Swift. Instantiate nib's view in 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.
So, I have: nib file with blank view where view is of type MyClass
and nib owner of type MyViewOwner
. Both classes written in Swift.
I will be posting only MyViewOwner class code since MyView.swift is just a class definition.
MyViewOwner.swift
class MyViewOwner {
@IBOutlet weak var myView: MyView
}
Caused error above.
Of course Swift objects are not KVO compliant, so I tried:
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
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; 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.
Perhaps what you mean is:
let myViewOwner = MyViewOwner() // or whatever the initializer is
NSBundle.mainBundle().loadNibNamed("MyView", owner: myViewOwner, options: nil)