IOS 学习笔记 “this class is not key value coding-compliant for the xxx”问题的解决

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值