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

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

这是什么意思?

我该怎么解决呢?

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


当前回答

我因为不同的原因遇到了同样的问题:我使用主故事板作为启动屏幕文件。我猜如果你使用一个故事板作为启动屏幕文件,它不应该连接到视图控制器,因为它还没有被加载。

其他回答

就我而言,

[[NSBundle mainBundle] loadNibNamed:@"NameOfTheSubviewNibFile" owner:self options:nil]

这就是原因。

将此替换为initWithNibName,已解决。

我只是在我的重复项目中有这个问题,并通过检查2个地方来解决:

1-确保你有。m文件在列表->项目-构建阶段-编译源代码 2-在那之后,去接口构建器(可能这是一个错误只发生在IB),并断开所有属性,标签,图像等…然后重新链接所有。我已经意识到我已经删除了一个属性,但它仍然在IB中链接。

希望这对一些人有用。

这个错误是另一回事!

这是我如何解决它。我使用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.

欢呼,

我发现的另一个“不兼容”问题是当我出于某种原因设法拥有两个类副本时。

我给错误的副本加了密钥。Interface Builder仍然看到键,并让我连接到它们,但在运行时,它正在使用没有新键的类的另一个副本。

为了找到“正确”的副本,我使用XCode的cmd-点击类名跳转到正确的副本,然后我杀死了不好的未使用的副本(先从未使用的副本中带来我的编辑)。

这个故事的寓意:重复的类文件是不好的。

通常当这种情况发生在我身上时,@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