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

Adr*_*lli 11 compiler-errors ios 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.'
Run Code Online (Sandbox Code Playgroud)

这是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
Run Code Online (Sandbox Code Playgroud)

这是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;
 }
Run Code Online (Sandbox Code Playgroud)

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

bgo*_*son 22

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

  • 如果右键单击IB中的项目以显示连接,则在已断开的连接旁边将显示"x",因此您可以删除它们.(是的,它应该生成编译时警告......也许它现在就做了;请检查你的输出.) (6认同)