1
votes

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.'

1

1 Answers

2
votes

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