I m getting error Terminating app due to uncaught exception
'NSUnknownKeyException', reason: '[<KGM_IOS.NotEkle 0x7fcb5ee0cd40> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key notEkleTextView.'.
I have XIB called "notEkle" and inside of "notEkle" i have UITextView and XIB provider is ViewController. When i connect this text with controller i got this error. I load my XIB in controller like
let notEkleView: UIView = NotEkle().loadNib() as UIView
I connect my text view with controller like
@IBOutlet weak var notEkleTextView: UITextView!
I dont have any problem before connect UITextview and controller.
let notEkleView: UIView = NotEkle().loadNib() as UIView
extension UIView {
func loadNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nibName = type(of: self).description().components(separatedBy: ".").last!
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiate(withOwner: self, options: nil).first as! UIView
}
}
There are several steps to define a XIB and use it programmatically.
First you need to create XIB file and the Swift file with the same name.
Design as you need, then hook up the file's owner as the class name you have defined in Step 1.
You need to define a common initializer method and override both initializers (with frame and coder).
Hook up the whole view in XIB as IBOutlet in Swift class.
Implement common initializer as follows:
private func commonInitializer() {
Bundle.main.loadNibNamed("testView", owner:self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight,.flexibleWidth]
}
For more details, you can check out this tutorial here
Everything was right for me but still I was getting this error, then I solved it by
This happens when outlets are not connected correctly from the storyboard into a view controller.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With