this class is not key value coding-compliant for the key
在学习的过程中,对于初学者这个问题肯定会遇到这个错误“this class is not key value coding-compliant for the key”,但是根据网上的解决办法基本上都只是前两种解决方案:
第一种:IBOutlet重命名了或者是Inteface Builder中的连线有两个
这种可能性是很大的
第二种:xib文件的File's Owner对应的Class文件错误
其实这种情况如果是在创建ViewController文件的时候勾选了同时创建xib,是不会有这个问题的,但是这里是一个检查点
第三种:AppDelegate
这种情况是最隐蔽的,对于初学者来说,先看一段代码:、
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//最后一步将创建xib文件绑定到window界面上
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UIViewController alloc]initWithNibName:@"RootViewController" bundle:nil];
[self.window makeKeyAndVisible];
return YES;
}
@end
这段代码是在去掉故事板使用xib布局的时候需要修改的地方,但是对于初学者来说,可能并不知道这是有问题的,而且如果遇到问题,还真是不好解决,会以为这里不是通过UIViewController alloc]initWithNibName:来加载RootViewController吗?没有问题啊,但是,就是因为这个问题造成了:“this class is not key value coding-compliant for the key”错误,原因就是:我们需要初始化的是RootViewController,而不是系统的UIViewController这个所有ViewController的顶级父类,因此改为一下代码就好了:
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//最后一步将创建xib文件绑定到window界面上
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil];
[self.window makeKeyAndVisible];
return YES;
}
@end