setValue:forUndefinedKey this class is not key value coding-compliant for the key

来源:互联网 发布:网络机顶盒电视猫下载 编辑:程序博客网 时间:2024/09/21 09:05

今天在写自定义UITableViewCell的时候遇到一个问题,xib 关联不了类,然后按照网上的步骤:通过设置Flie`s Owner 的custom class 关联上去 ,但是运行直接Crash,报下面的错❌

❎Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key KindsListName.’

然后百度了一下参照这个:点我✅

发现设置完成后还是报上面的错误:表情(一脸的懵逼)(⊙﹏⊙)b

setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key KindsListName.’

定位错误原因应该还是自定义xib的问题。

不得已又去:stack overflow 翻看几个谈论如下:PS看不懂或者不想看直接死命往下翻

There are a couple of options to resolve this - i’ll let you decide which is the most appropriate.

The reason it’s failing is because the owner is being passed as nil. You’re binding the actionText outlet to the file’s owner in IB, but then when loading the nib, the owner is nil. I’d guess that when loading with a nil owner behind the scenes an NSObject is used, which is why you’re getting the key/value error.

My previous advice to pass the cell as the owner would also fail as I didn’t know how the Nib is constructed; the cell is nil as you’ve yet to create it (and dequeue is passing nil back, so even pass cell as the owner is still essentially passing nil).

Two options:

Instantiate a new cell in your -cellForRowAtIndexPath:(NSIndexPath *)indexPath implementation, and pass that new cell as the owner (but i’d guess that this isn’t the best solution for you)
Or, and I’d suggest this is the better solution, change the binding of actionText in your nib file to the Alert Cell and not the file’s owner (You have File’s Owner and an Alert Cell - bind the UILabel to the actionText outlet of the Alert Cell, and not the File’s owner, which is what’s being done at present) - I suspect this is what you want. With that in mind file’s owner can become an NSObject again.
——- Original answer kept below as the file’s owner class is also a common cause for this error ——-

It suggests that you’ve ‘instantiated’ an AlertCell in InterfaceBuilder, and you’re binding something to actiontext, but the class isn’t set to AlertCell, it’s still NSObject?

Take a look at the class text box on the identify tab of the tool palette for that object in Interface Builder. The class should be AlertCell, but i’d guess it’s still set to NSObject.

As an aside, and feel free to ignore this advice, but there are a couple of extra things i’d encourage you to do, purely from an Objective C expectations/conventions point of view:

Name your files after your class (upper case the first character of the filename).
Prefix your class names; two uppercase characters, typically your initials (i’d name it DWAlertCell, for example).

大概说的意思问题原因在:MenuTVCell.h 的 outlet 上面。

然后又重新检查了一下自己的控件是不是关联出了问题:

解决办法

果不其然,上面的KindsListName 关联了一个File`s Owner 跟MenuTV Cell 删除一个再运行: OK!

分析原因:

第一次关联的File`s Owne忘记删除了,导致后面的问题。

0 0