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
    }
}
2

7 Answers 7

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

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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

Comments

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

2 Comments

I cheked but its connected. The circle which the left side of @IBOutlet weak var notEkleTextView: UITextView! is filled
have you checked classes file owners etc also simply use: Bundle.main.loadNibNamed("yournibname", owner:self, options: nil) do not make it complex
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

Comments

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"

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.