Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load XIB this class is not key value coding-compliant for the key

I just create custom UIView with name PopupViewForViewMoreDetail and I want to add this custom view in my ViewController but each time getting below error

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<0x7f8155f2e430> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key mainView.'

if I remove IBOutlet of mainView then It will be display error for another variable.

Below is my code

import UIKit

class PopupViewForViewMoreDetail: UIView {

    @IBOutlet var darkBGView: UIView!
    @IBOutlet var outerView: UIView!
    @IBOutlet var mainView: UIView!
    @IBOutlet var btnClose: UIButton!

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

and I'm adding this view in my viewController class like below.

 let viewMoreDetailPopUp =  Bundle.main.loadNibNamed("PopupViewForViewMoreDetail", owner: self, options: nil)?.first as! PopupViewForViewMoreDetail
        self.view.addSubview(viewMoreDetailPopUp)

Below is my screenshot for IBOutlets

enter image description here

I also checked below answer but didn't help me.

What does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X"

Guide me where I'm going wrong in my code ?

like image 838
Govaadiyo Avatar asked Jun 27 '18 06:06

Govaadiyo


People also ask

Why is my class not key value coding-compliant?

Even thought Xcode looks so clean and simple there are lots of hidden features and it takes time to find and learn all these things. So whenever you get the error: “this class is not key value coding-compliant” you should first and foremost think if you recently changed something that has a connection to the Main.storyboard or to the design file.

Why does my xib file keep crashing?

This is caused by one of the following issues, the Storyboard or the XIB file is expecting to be connected to an IBOutlet and during run time it is not able to find that outlet so it crashes. It could also be due to the Storyboard referencing the incorrect class. The error will look something like this:

Why is my IBOutlet not working in Xcode?

Unfortunately most of the time one might close the Connections inspector or you might only have the view controller code open, if that is the case you can easily miss this issue due to Xcode not showing that there is an issue with the IBOutlet when you are in an editor window.


2 Answers

try this :

step 1:

let viewMoreDetailPopUp =  UINib(nibName: "PopupViewForViewMoreDetail", bundle: nil).instantiate(withOwner: self, options: nil).first as! PopupViewForViewMoreDetail

step 2: make sure you're setting class for the view instead of File's Ownerenter image description here

enter image description here

Step 3 : Remove all outlets and re-outlet again

like image 148
Tung Vu Duc Avatar answered Sep 21 '22 13:09

Tung Vu Duc


For me it was that I copied a xib to make a new variation and it unchecked inherit module from target.

like image 44
Eric Mentele Avatar answered Sep 18 '22 13:09

Eric Mentele