我在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,将旧的扔进了垃圾箱,但是问题仍然存在。

最佳答案

当没有正确建立Interface Builder或Storyboard连接时,通常会发生这种情况。有时您会建立连接,然后删除建立连接的代码。 Interface Builder仍然具有对代码的引用,这会导致键/值兼容的运行时错误。如果您没有将适当的类分配给 View Controller ,您也可能会收到此错误。如果您已经为特定的 View Controller 编写了代码,请确保在Interface Builder中为该 View Controller 适本地设置类。

09-04 16:44
查看更多