*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x198620> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key myBtn.'
*** Call stack at first throw:
并且xcode代码运行到UIApplicationDelegate的 [self.window makeKeyAndVisible];时停止,提示program received signal “SIGABRT”
这个问题的根本原因是在xib中某个控件已经通过outlet连接到ViewController中的某个变量上了,后来变量删除了,但是这种连接关系没有删除,导致启动时找不到对应的key,抛出上述exception。
解决办法如下:
找到该controller对应的xib,右键查看某个控件关联的属性是否已经不存在:
如下图:
如不存在,点击上图中的红色线上的叉号,删除关联,然后重新拉线。
【注:有时候在上图中看不到关联,依旧重新拉线即可】
【注意重新拉线后,定义的变量名要和删除前的变量名一致。如果忘记变量名,只需在警告中找到,比如上方的:“Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x198620> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key myBtn.'”可以在末尾看出变量名就是'myBtn'】
(文章出处:http://www.xuebuyuan.com/1353520.html,有修改)