19

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 ?

2
  • Change owner: self to owner: nil
    – matt
    Jun 27, 2018 at 7:14
  • I changed but not working
    – Govaadiyo
    Jun 27, 2018 at 7:18

2 Answers 2

56

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

8
  • 22
    Connecting the outlet to the view, and not the file owner, was the trick. Thanks!
    – Crashalot
    Dec 13, 2018 at 3:49
  • @TungVuDuc can you explain why setting the view's class rather than the file's owner's one is required here? Dec 28, 2018 at 14:11
  • 1
    @Johnson_145 because when you creating a view from nib, their subviews belong to its view not file's owner Dec 31, 2018 at 5:54
  • 1
    Thank you. save my time. Mar 28, 2019 at 7:24
  • 4
    This is the only solution that works! Beware: After doing the things @TungVuDuc described and still get the same error, check the outlets pointing from the File's Owner. You may need to remove them in case you created them while trying to get this to work! May 30, 2019 at 15:37
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.