如果视图控制器位于不同项目的库和故事板中,则无法解决“NSUnknownKeyException”错误(Unable to solve the 'NSUnknownKeyException' error if view controllers are in a library and storyboard in different project)

问题:

按钮单击时出现以下错误

2014-06-05 13:19:28.118 Generic-Project [277:60b] Interface Builder文件中的未知类MySecondViewController。

2014-06-05 13:19:28.134 Generic-Project [277:60b] ***由于未捕获的异常'NSUnknownKeyException'终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不符合键值编码对于关键lblStatus。'

支持意见:

lblStatus是新视图控制器中的UILabel ,我试图在按钮点击时打开它。 视图控制器的自定义类是正确的,并且此标签仅连接到新视图控制器中的以下属性:

@property (weak, nonatomic) IBOutlet UILabel *lblStatus;

请注意, 包括根视图控制器在内的所有视图控制器都在我们创建的库中。 故事板在一个不同的项目中。 我已经在项目的General设置中添加了对Linked Framework and Libraries选项的库引用,以告诉项目使用该库。 根视图控制器显示正常但当我单击它上面的按钮打开第二个视图控制器时,它会给出错误。

如果我将第二个视图控制器从库移动到项目,或者我只是将库中的.m文件添加到项目的Build Phases中的Compile Sources ,则一切正常。

我尝试过的:

我试过以下答案:

我如何解决'NSUnknownKeyException'... setValue:forUndefinedKey:]:...不符合键值编码

对于关键视图,此类不符合键值编码

我很感激你的意见。 我是iOS的初学者,一直试图解决这个难题。

Problem:

I am getting a following error on Button click:

2014-06-05 13:19:28.118 Generic-Project[277:60b] Unknown class MySecondViewController in Interface Builder file.

2014-06-05 13:19:28.134 Generic-Project[277:60b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key lblStatus.'

Supporting comments:

lblStatus is a UILabel inside the new view controller which I am trying to open on button click. The custom class of the view controller is correct and the this label is only connected to the following property in the new view controller:

@property (weak, nonatomic) IBOutlet UILabel *lblStatus;

Just a note, all the View Controllers including the root view controller is in a library that we've created. And the storyboard is in a different project. I've added the library reference to Linked Framework and Libraries option in General settings of the project in order to tell the project to use the library. Root view controller shows fine but when I click the button on it to open the second view controller, it gives the error.

All works fine if either I move the second view controller from the library to the project or if I just add the .m file from the library to the Compile Sources in Build Phases of the project.

What I've tried:

I've tried the answers on following:

How do I solve a 'NSUnknownKeyException' ... setValue:forUndefinedKey:]: ...not key value coding compliant

this class is not key value coding-compliant for the key view

I'll appreciate your input. I am a beginner in iOS and have been trying to solve the puzzle since a day.

最满意答案

您需要在项目的Build Settings中的“Other Linker Flags”中使用-ObjC标志。 这使链接器可以在库中构建所有内容。

它在Apple静态库中提到了如何说:

此标志将告诉链接器将所有Objective-C类和类别从静态库链接到您的应用程序,即使链接器无法告知它们已被使用。

You need to use the -ObjC flag in "Other Linker Flags" in the project's Build Settings. This gets the linker to build everything in the library.

It is mentioned in the Apple static library how-to which says:

This flag will tell the linker to link all Objective-C classes and categories from static libraries into your application, even if the linker can’t tell that they are used.

更多推荐