12

我在 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,把旧的扔进了垃圾桶,但问题仍然存在。提前致谢

4

2 回答 2

23

这通常发生在未正确建立 Interface Builder 或 Storyboard 连接时。有时你会建立一个连接,然后删除建立连接的代码。Interface Builder 仍然有对代码的引用,这会导致符合键/值的运行时错误。如果您没有为视图控制器分配正确的类,您也可能会收到此错误。如果您为特定的视图控制器编写了代码,请确保在 Interface Builder 中为该视图控制器适当地设置类。

于 2012-10-04T14:16:44.857 回答
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/

于 2015-03-29T22:12:50.730 回答