I have a xib file which I have been able to load successfully:
let xib = MyBundle.bundle.loadNibNamed("MyXib", owner: self, options: nil)!
let confirmView = xib[0] as! MyXib
This will load and appear correctly on screen.
The class is set correctly in the xib and it casts correctly when checked in code. The file owner is also set correctly.
However, if I ever drag an object from the xib file to its class (e.g. set a button reference) then the app will crash as soon as this xib is loaded.
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: [MyXib 0x7fcaf0d3eb50 setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key button.
The init is called:
required init?(coder: NSCoder) {
super.init(coder: coder)
}
Is called so I know it finishes initializing.
There are no broken outlets or anything funky. I am literally just dragging a button and creating an outlet and then running. If I delete the outlet it will work. If I add any outlet again to anything it will crash with this same issue.
Any suggestions as to what else might be the cause? I've already looked through the main causes of this.
Solution
As suggested below I removed the file owner, deleted all the outlets and used the exact loading syntax DonMag suggested.