1

I have a question about loading view from XIB programmatically and using outlets:

I created class for XIB's view:

class VoiceRecView: UIView {

    @IBOutlet weak var recButton: UIButton!

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    @IBAction func recButtonTapped(_ sender: Any) {
        print("rec button tapped")
    }
}

In the XIB I set file owner's class to my view controller and view class to "VoiceRecView".
In the view controller I add the XIB as subview:

let voiceRecView = UINib(nibName: "VoiceRecView", bundle: nil).instantiate(withOwner: self, options: nil)[0] as! VoiceRecView
self.view.addSubview(voiceRecView)

And when I run the app I get the error:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<....ChatViewController 0x7fd0a27077e0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key recButton.'

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Elena Rubilova
  • 367
  • 4
  • 16
  • 1
    https://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-this-class-is-not-key-v – Nitish Feb 03 '18 at 12:52
  • Possible duplicate of [What does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X"](https://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-this-class-is-not-key-v) – Nitish Feb 03 '18 at 13:01

1 Answers1

2

the error was my fault, fixed by deleting and reconnecting an outlet.

Elena Rubilova
  • 367
  • 4
  • 16