5

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
    }
}
Yannick Loriot
  • 7,107
  • 2
  • 33
  • 56
burak kaya
  • 105
  • 1
  • 1
  • 6
  • You'd better write down your code lines between ` letters in order to increase readability of your question. – Yusuf Kamil AK Jan 30 '19 at 07:22
  • 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) – Cristik Jan 30 '19 at 08:36

7 Answers7

3

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

Yusuf Kamil AK
  • 771
  • 8
  • 17
3

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
  • This needs to be done for the xib object itself. There is also a place to fill this in the same info on the File's Owner (in the Placeholders section above the object), but that won't solve the problem. – Andy Weinstein Aug 30 '20 at 19:09
1

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

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.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
vilas deshmukh
  • 389
  • 2
  • 5
0

Just after declaration fo class connect outlet like drag it from the xib file to the class(owner) and it should be like:

@IBOutlet weak var notEkleTextView: UITextView!

as in your case it is not working that means you have given some other class

Your view controller might be having the wrong class in your xib. Check the file owner and in VC check the file inspector if classes have been assigned properly

Nishant Pathania
  • 349
  • 1
  • 14
  • I cheked but its connected. The circle which the left side of @IBOutlet weak var notEkleTextView: UITextView! is filled – burak kaya Jan 30 '19 at 07:19
  • have you checked classes file owners etc also simply use: Bundle.main.loadNibNamed("yournibname", owner:self, options: nil) do not make it complex – Nishant Pathania Jan 30 '19 at 07:47
0

Please make sure following:

  1. Inside xib, set Base Class of your view to NotEkle.
  2. Instantiate like this, let notEkleView = NotEkle().loadNib() as! NotEkle
axunic
  • 2,256
  • 18
  • 18
0

Make sure that you entered correct "nibName" inside init function ScreentShot Attached

init(pages : [Page],holder : UIView) {
    self.pages = pages
    self.holder = holder
    super.init(nibName: "PagerViewNew", bundle: nil)
}

I put "PagerView" Instead of "PagerViewNew"

Taimoor Suleman
  • 1,588
  • 14
  • 29
0

I rebuilt my project and added the storyboards in I got and got this error. I solved it by selecting the module to the correct project. Storyboard > ViewController > Identity Inspector > module