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
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 ?
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.
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:
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.
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 Owner
Step 3 : Remove all outlets and re-outlet again
For me it was that I copied a xib to make a new variation and it unchecked inherit module from target.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With