这个类不符合键值编码的要求,针对键"view"。

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,抛弃了旧的,但问题仍然存在。谢谢提前。

2个回答

23

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


6
在交易软件IB中,如果你右键单击物品以显示连接,那么在断开连接的物品旁边会有一个"x",这样你就可以删除它们。(实际上最好生成编译时警告,也许现在已经这样了,所以请检查你的输出。) - Rob Napier

4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接