我试图将UILabel与在类中创建的IBOutlet链接起来。
我的应用程序崩溃与以下错误。
这是什么意思?
我该怎么解决呢?
***终止应用由于未捕获异常'NSUnknownKeyException',原因:'[<UIViewController 0x6e36ae0> setValue:forUndefinedKey:]:这个类不是键值编码兼容的键XXX。'
我试图将UILabel与在类中创建的IBOutlet链接起来。
我的应用程序崩溃与以下错误。
这是什么意思?
我该怎么解决呢?
***终止应用由于未捕获异常'NSUnknownKeyException',原因:'[<UIViewController 0x6e36ae0> setValue:forUndefinedKey:]:这个类不是键值编码兼容的键XXX。'
当前回答
如果你有一个带有iboutlet的自定义UIViewController子类,它会导致问题,我发现唯一能消除错误的步骤是
1 .修改类为UIViewController
断开所有的插座(它们现在都有黄色警告三角形)-仅仅断开有问题的插座可能就足够了。
.3执行所有标准步骤-↑⌘K,删除派生数据(,祈祷垫,担忧珠)
.4启动应用程序-进入有问题的场景。
.5关闭应用程序,返回接口生成器,将类更改为自定义类名。
6重新连接电源插座。
启动应用程序&这通常会清除键值遵从性问题。
其他回答
我发现了另一种可能导致问题的可能性。
当使用initWithNibName方法使用错误的xib名称时,也会导致这种崩溃。
例如,您选择更改一个xib文件的名称,然后忘记将“initWithNibName”方法中使用的名称更改为相同的名称。
如果你有一个带有iboutlet的自定义UIViewController子类,它会导致问题,我发现唯一能消除错误的步骤是
1 .修改类为UIViewController
断开所有的插座(它们现在都有黄色警告三角形)-仅仅断开有问题的插座可能就足够了。
.3执行所有标准步骤-↑⌘K,删除派生数据(,祈祷垫,担忧珠)
.4启动应用程序-进入有问题的场景。
.5关闭应用程序,返回接口生成器,将类更改为自定义类名。
6重新连接电源插座。
启动应用程序&这通常会清除键值遵从性问题。
我得到了同样的错误,当我手动加载一个视图从一个笔尖。结果是我忘记设置视图的所有者。
例如,如果你以以下方式加载一个视图,
let view = Bundle.main.loadNibNamed("MyNibFileName",
owner: nil,
options: nil)?.first as! UIView
然后添加一个IBOutlet, IBOutlet不能被引用,应用程序会因为上面的错误而崩溃。
修复:为视图分配一个所有者。
let view = Bundle.main.loadNibNamed("MyNibFileName",
owner: self,
options: nil)?.first as! UIView
这个错误是另一回事!
这是我如何解决它。我使用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.
欢呼,
就我而言,
[[NSBundle mainBundle] loadNibNamed:@"NameOfTheSubviewNibFile" owner:self options:nil]
这就是原因。
将此替换为initWithNibName,已解决。