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

时间:2012-10-04 14:06:52

标签: ios compiler-errors ios-universal-app appdelegate

我在AppDelegate中遇到问题,在运行应用程序时出现此错误:

  Terminating app due to uncaught exception 'NSUnknownKeyException', reason: 
  '[<UIApplication 0x856c820> setValue:forUndefinedKey:]:
   this class is not key value coding-compliant for the key view.'

这是AppDelegate.h的代码

#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>{

        //UINavigationController *navigationController;
 }

@property (strong, nonatomic) UIWindow *window;


@property (copy, nonatomic) ViewController * viewController;
@property (copy, nonatomic) UINavigationController * navigationController;



 @end

这是AppDelegate.m的代码

 #import "AppDelegate.h"

 #import "RootViewController.h"



  @implementation AppDelegate



  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:  (NSDictionary *)launchOptions
   {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        RootViewController *rootMenu;


         if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
              rootMenu= [[RootViewController alloc]  initWithNibName:@"ViewController_iPhone" bundle:nil];
    } else {
              rootMenu = [[RootViewController alloc]initWithNibName:@"ViewController_iPad" bundle:nil];
  }


  self.navigationController =[[UINavigationController  alloc]initWithRootViewController:rootMenu];

  self.window.rootViewController = self.navigationController;

  [self.window makeKeyAndVisible];
   return YES;
 }

我该怎么做才能解决此错误?我已经重写了RootViewController,扔掉了旧的垃圾桶,但问题仍然存在。谢谢提前

2 个答案:

答案 0 :(得分:22)

这通常在未正确创建Interface Builder或Storyboard连接时发生。有时您将建立连接,然后删除建立连接的代码。 Interface Builder仍然具有对代码的引用,这会导致键/值兼容的运行时错误。如果尚未将适当的类分配给视图控制器,也可能会出现此错误。如果您已为特定视图控制器编写代码,请确保在Interface Builder中为该视图控制器正确设置类。

答案 1 :(得分:4)

我知道这个问题有点陈旧,但我遇到了同样的问题,下面的文章帮了很多忙。基本上它逐步说明了如何解决@bgolson描述的问题。

http://www.tech-recipes.com/rx/52021/how-do-i-fix-the-issue-this-class-is-not-key-value-coding-compliant-for-the-key-in-xcode-6/