18

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 ?

Govaadiyo
  • 5,644
  • 9
  • 43
  • 72

2 Answers2

55

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

Tung Vu Duc
  • 1,552
  • 1
  • 13
  • 22
  • 21
    Connecting the outlet to the view, and not the file owner, was the trick. Thanks! – Crashalot Dec 13 '18 at 03:49
  • @TungVuDuc can you explain *why* setting the view's class rather than the file's owner's one is required here? – Johnson_145 Dec 28 '18 at 14:11
  • 1
    @Johnson_145 because when you creating a `view` from nib, their subviews belong to its `view` not file's owner – Tung Vu Duc Dec 31 '18 at 05:54
  • 1
    Thank you. save my time. – Antony Wong Mar 28 '19 at 07: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! – Bogdan Razvan May 30 '19 at 15:37
  • @Crashalot Exactly! remove all outlets and remove class from "File's Owner". Then add class for "view". Without removing previous outlets or both selected class create same issue also. – Michael42 Jun 30 '19 at 21:11
  • Setting the class for view instead of File Owner means the view outlet must be removed, and then I get this error: `loaded the "CustomNibName" nib but the view outlet was not set.'` – jcharch Jul 15 '19 at 13:32
  • Thanks saved a lot of time, I was trying to use file owner however not a chance. – Gustavo de Souza Oct 11 '19 at 16:02
2

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

Eric Mentele
  • 864
  • 9
  • 16