esteven1234
esteven1234

Reputation: 31

Generating UINib instance of custom UIView giving 'not key value coding-compliant' error

I'm attempting to use a custom uiview for a tinder-style card swipe library called Koloda, which requires that I pass in a uiview for each card in the stack. I'm attempting to use a custom uiview with relevant fields/info, and after thorough testing I've discovered that generating an instance of uinib is giving me a 'not key value coding-compliant' error, despite having all my outlet connections in order.

I've tried recreating all my IBOutlets, going as far as to just start over with a new xib file with new connections. When I drag a uiview onto a view controller in storyboard, and conform it to my custom type, it shows up no problem and I can manipulate its various properties as desired. It's only when instantiating the custom view that I get the error.

class VoteCard: UIView {

@IBOutlet var contentView: UIView!
@IBOutlet weak var proPicImg: UIImageView!
@IBOutlet weak var nameLbl: UILabel!
@IBOutlet weak var homeLbl: UILabel!
@IBOutlet weak var ratingLbl: UILabel!

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

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

func commonInit() {
    Bundle.main.loadNibNamed("VoteCard", owner: self, options: nil)
    contentView.fixInView(self)
}

class func instanceOf() -> VoteCard {
    return UINib(nibName: "VoteCard", bundle: nil).instantiate(withOwner: nil, options: nil).first! as! VoteCard
}

}

If all works correctly, my Koloda card stack should show my custom cards, however as of yet I can't get passed the generation of a new instance.

Upvotes: 0

Views: 375

Answers (2)

Chanchal Chauhan
Chanchal Chauhan

Reputation: 1560

There might be some outlets which was define first and then removed from your class. But they are not removed from your storyboard or xib. Please check for those outlets in your view and remove them. Please refer the image. Here buttonShow is not in my view controller so remove it.

enter image description here

Hope it helps.

Upvotes: 0

axunic
axunic

Reputation: 2316

Please see this answer: https://stackoverflow.com/a/52831749/2537616

Here is the idea:

  1. Set the BaseClass of your custom view inside .xib file.
  2. Connect outlet as usual.
  3. Then create static helper to instantiate UINib.

Upvotes: 0

Related Questions