我试图将UILabel与在类中创建的IBOutlet链接起来。

我的应用程序崩溃与以下错误。

这是什么意思?

我该怎么解决呢?

***终止应用由于未捕获异常'NSUnknownKeyException',原因:'[<UIViewController 0x6e36ae0> setValue:forUndefinedKey:]:这个类不是键值编码兼容的键XXX。'


当前回答

我在为表视图原型单元创建的自定义类的所有出口上都遇到了这个错误。出口都正确地连接到故事板,并且Class字段在原型单元格的标识检查器中被正确地命名。

删除和重新创建出口都不起作用。清理构建没有起作用。最后有效的方法是将故事板标识检查器中的Class更改为默认的UITableViewCell,点击Enter,然后将其更改为自定义类的名称。出于某种原因,这奏效了。

其他回答

通常当这种情况发生在我身上时,@TechZen的回答很管用。然而,昨天我花了很长时间处理故事板连接,却发现问题出在我的代码上。

我有一个自定义视图控制器来处理故事板中的各种布局,但其中一个布局需要一个其他布局不使用的特殊标签。所以我创建了一个子类,像这样:

@interface MyViewControllerSubclass : MyViewController

然后我在MyViewControllerSubclass.m中添加了一个私有属性:

@interface MyViewController ()
@property (weak, nonatomic) IBOutlet UILabel *crashesApp;
@end

Xcode很高兴地允许我连接这个IBOutlet,但每次视图加载时,应用程序都会崩溃,因为旧的“不key-value兼容键'chrashesApp'”。

解决方案,回顾起来是半明显的,就是改变私有类别以使用正确的名称,即子类的名称:

@interface MyViewControllerSubclass ()
@property (weak, nonatomic) IBOutlet UILabel *noMoreCrashing;
@end

你的xib可能连接不好。

这个错误我犯过很多次了。虽然TechZen的答案在这种情况下是绝对正确的,但另一个常见的原因是当您更改.h/中的IBOutlet属性的名称时。m,你已经连接到文件的所有者在nib。

从你的笔尖:

在IB中选择对象,然后转到“连接检查器”。 在“引用Outlets”下,确保你的对象没有仍然连接到旧的属性名…如果是,单击小的“x”删除引用并重新构建。

另一个常见的原因是,如果你使用Storyboard,你的UIButton可能有多个赋值(解决方案几乎与nib相同):

打开你的故事板,右击UIButton 您将看到该按钮有多个assign/ref。 删除一个带有小“x”的灰色“Main…”窗口:

"这个类不符合键值编码" 我知道有点晚了,但我的答案是不同的,所以我认为它需要张贴,我推错了第二个控制器的方式,这里是样本

错误的推控制器方式

UIViewController* controller = [[UIViewController
 alloc]initWithNibName:@"TempViewController" bundle:nil];
         [self.navigationController pushViewController:controller animated:true];

正确的方法

TempViewController* controller = [[TempViewController
 alloc]initWithNibName:@"TempViewController" bundle:nil];
         [self.navigationController pushViewController:controller animated:true];

我没有找到如上的答案,所以它可能可以帮助一些有同样问题的人

当我试图实现一个表的自定义ViewCell时,我有这个错误。当我突出显示XIB的视图控制器并连接到CellView中的元素时,会导致错误“这个类不是键值编码兼容的键”,一旦我删除了这些,它就摆脱了错误。

删除下图中的连接。

只要确保你只有与表格视图单元格的连接。要检查,单击表视图单元格,并在检查器中寻找您的连接。

这个错误是另一回事!

这是我如何解决它。我使用xcode版本6.1.1和使用swift。每次我的应用程序尝试执行segue跳转到下一个屏幕时,我都得到这个错误。这是我所做的。

Checked that the button was connected to the right action.(This wasn't the problem, but still good to check) Check that the button does not have any additional actions or outlets that you may have created by mistake. (This wasn't the problem, but still good to check) Check the logs and make sure that all the buttons in the NEXT SCREEN have the correct actions, and if there are any segues, make sure that they have a unique identifier. (This was the problem) One of the segues did not have a unique identifier One of the buttons had an action and two outlets that I created by mistake. Delete any additional outlets and make sure that you the segues to the next screen have unique identifiers.

欢呼,