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

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

这是什么意思?

我该怎么解决呢?

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


当前回答

您只需要指定IBOutlet一次,IBOutlet标签您的ivar是不必要的。 你在用你的UIViewController实例化你的NIB吗?在某些时候,你应该调用[SecondView initWithNibName:@"yourNibName" bundle:nil];

其他回答

就我而言,

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

这就是原因。

将此替换为initWithNibName,已解决。

对我来说。合并后,xib文件中没有丢失的输出。

Shift + Command + K

解决了我的问题。我清理了我的项目并重建。

您只需要指定IBOutlet一次,IBOutlet标签您的ivar是不必要的。 你在用你的UIViewController实例化你的NIB吗?在某些时候,你应该调用[SecondView initWithNibName:@"yourNibName" bundle:nil];

我从头文件中删除了该属性。我找不到它的任何引用,但调试错误仍然引用它。我发现nib文件仍然有对它的引用。我删除了引用它的块,一切都修复了。

在Project Navigator中,

找到Nib (xib)文件。右键单击并查看源。我删除了下面的完整部分

<object class="IBConnectionRecord">
    <object class="IBCocoaTouchOutletConnection" key="connection">
        <string key="label">DeleteLabel</string>
        <reference key="source" ref="372490531"/>
        <reference key="destination" ref="774585933"/>
    </object>
    <int key="connectionID">20</int>
</object>

你的视图控制器可能在xib中有错误的类。

我下载了你的项目。

你得到的错误是

'NSUnknownKeyException',原因:'[<UIViewController 0x3927310> setValue:forUndefinedKey:]:这个类不是键值编码兼容的键字符串。'

这是由主窗口中的第二个视图控制器引起的。xib有一个UIViewController类而不是SecondView。更改到正确的类可以解决这个问题。

顺便说一下,在Objective-C中使用"string"这样的名字是不好的做法。它会引起运行时命名冲突。即使是在一次性练习应用程序中也要避免使用它们。命名冲突很难追踪,你不想浪费时间。

这个错误的另一个可能的原因是:当从一个控制器复制和粘贴元素到另一个控制器时,Xcode以某种方式保留了原始控制器的链接,即使在编辑和重新链接这个元素到新的控制器之后。

造成这个错误的另一个可能原因是:

糟糕的出口。

您在.h文件中删除或重命名了一个出口名称。

在.xib或.storyboard文件的连接检查器中删除它。

还有一个可能的原因

(在我的情况下)扩展带有可绑定属性的UIView,并为这些可绑定属性设置值(即阴影,角半径等),然后从UIView扩展中删除这些属性(出于某种原因),但以下<userDefinedRuntimeAttributes>保留在xml (of foo.storyboard)中:

<userDefinedRuntimeAttributes>
  <userDefinedRuntimeAttribute type="color" keyPath="shadowColor">
      <color key="value" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
  </userDefinedRuntimeAttribute>
  <userDefinedRuntimeAttribute type="number" keyPath="shadowOpacity">
      <real key="value" value="50"/>
  </userDefinedRuntimeAttribute>
  <userDefinedRuntimeAttribute type="point" keyPath="shadowOffset">
      <point key="value" x="5" y="5"/>
  </userDefinedRuntimeAttribute>
  <userDefinedRuntimeAttribute type="number" keyPath="shadowRadius">
      <real key="value" value="16"/>
  </userDefinedRuntimeAttribute>
  <userDefinedRuntimeAttribute type="number" keyPath="borderWidthValue">
      <real key="value" value="0.0"/>
  </userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>

解决方案:右键单击foo。删除导致问题的</userDefinedRuntimeAttributes