2

I wrote a custom view by xib in swift. The view shows correct and the @IBAction is also right. But when I add an @IBOutlet, the app will crash.

The error message is:

2016-03-15 22:32:16.650 ****[42692:1015001] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x7f8ea483cd30> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key cancelBtn.'

And the xib:

XIB Information

And the code is:

 class PYChangeNameView: UIView { 
  var confirmAction: ((name: String?) -> ())? 
  var cancelAction: (() -> ())? 

  @IBOutlet weak var cancelBtn: UIButton! 

  private init() { 
   super.init(frame: CGRectZero) 
   if let view = UIView.loadFromNibNamed("PYChangeNameView") {    
    view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] 
    self.frame = UIScreen.mainScreen().bounds self.addSubview(view) 
   } 
  }

  @IBAction func cancelClick(sender: AnyObject) {
    PYCNVStaticValues.sharedInstance?.removeFromSuperview()
    PYCNVStaticValues.cancelAction?()
  }
}

The @IBAction works, but the @IBOutlet does not work. }

Hugo Alonso
  • 6,684
  • 2
  • 34
  • 65
Pluto Y
  • 61
  • 1
  • 4
  • Is the button actually named "cancelBtn" in the xib? Based on the action name and the error message, I would guess it is named "cancel". – Wayne Tanner Mar 15 '16 at 14:47
  • You can see the picture, the button is named "cancelBtn". – Pluto Y Mar 15 '16 at 14:51
  • Have you added an IBOutlet to this button in any other place? – Hugo Alonso Mar 15 '16 at 14:58
  • @HugoAlonso No, I had search the key "cancelBtn" in the project, only this place. – Pluto Y Mar 15 '16 at 15:00
  • try and remove the IBOutlet from the inspector in this xib, rebuild, launch, if everything is OK, then set it back – Hugo Alonso Mar 15 '16 at 15:05
  • @HugoAlonso I had tried this method, it not works for me. – Pluto Y Mar 15 '16 at 15:08
  • So, if you remove the IBOutlet from the inspector it keeps giving you the error? – Hugo Alonso Mar 15 '16 at 15:09
  • 1
    Isn't it funny that is says `NSObject...setValue:forUndefinedKey:` instead of for instance `UIViewController...setValue:forUndefinedKey:`. What does your files owner look like? – pbodsk Mar 15 '16 at 15:10
  • @HugoAlonso If i remove the `@IBOutlet`, it run right. But if I add `@IBOutlet`, it crashed. – Pluto Y Mar 15 '16 at 15:20
  • @pbodsk ```class PYChangeNameView: UIView { var confirmAction: ((name: String?) -> ())? var cancelAction: (() -> ())? @IBOutlet weak var cancelBtn: UIButton! private init() { super.init(frame: CGRectZero) if let view = UIView.loadFromNibNamed("PYChangeNameView") { view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] self.frame = UIScreen.mainScreen().bounds self.addSubview(view) } } – Pluto Y Mar 15 '16 at 15:20
  • @pbodsk This is the content, It's a subclass of the `UIView`. – Pluto Y Mar 15 '16 at 15:21
  • Try this approach and see if it works for you http://stackoverflow.com/a/24370563/2683201 – Hugo Alonso Mar 15 '16 at 15:54
  • @HugoAlonso Yes, this is my way. – Pluto Y Mar 15 '16 at 16:02
  • @PlutoY has this worked for you? – QED Mar 18 '16 at 11:32

2 Answers2

3

Take a look at the error: it's telling you that NSObject doesn't have a cancelBtn field. And why would it?

Looking at your screenshot, I can see that you have the cancelBtn outlet set on your File's Owner. (The outlet should be set to whatever is your custom view.)

Therefore I can conclude that your File's Owner object isn't getting properly associated with your PYChangeNameView, and the runtime thinks it's simply an NSObject. I believe you need to set that class as the type in your XIB - probably in the controls at right.

Check out this beautiful Retina-quality screenshot that I captured:

Beautiful Screenshot at Retina Quality

There may also be a need to set the Module field. You may get some insight from this answer.

Community
  • 1
  • 1
QED
  • 9,803
  • 7
  • 50
  • 87
0

check to make sure that your view's xib is a PYChangeNameView

like This

Dan Leonard
  • 3,325
  • 1
  • 20
  • 32