Search code examples
iosswiftcocoa-touchxib

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 ?


Solution

  • 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