Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift this class is not key value coding-compliant for the key Error

Tags:

ios

swift

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
    }
}
like image 442
burak kaya Avatar asked Jan 30 '19 07:01

burak kaya


Video Answer


3 Answers

There are several steps to define a XIB and use it programmatically.

  1. First you need to create XIB file and the Swift file with the same name.

  2. Design as you need, then hook up the file's owner as the class name you have defined in Step 1.

  3. You need to define a common initializer method and override both initializers (with frame and coder).

  4. Hook up the whole view in XIB as IBOutlet in Swift class.

  5. 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]
}
  1. Finally, you can drag a UIView to anywhere and set its class as this custom class.

For more details, you can check out this tutorial here

like image 78
Yusuf Kamil AK Avatar answered Nov 14 '22 22:11

Yusuf Kamil AK


Everything was right for me but still I was getting this error, then I solved it by

  • Enabling the "Inherit module from Target" in the Identity Inspector. or
  • Giving the module(usually folder name/group name) in which it is present from the Identity Inspector under Custom Class
like image 34
Talib Shabbir Hussain Avatar answered Nov 14 '22 22:11

Talib Shabbir Hussain


This class is not key value coding-compliant for the key

This happens when outlets are not connected correctly from the storyboard into a view controller.

like image 28
vilas deshmukh Avatar answered Nov 14 '22 20:11

vilas deshmukh