0

我正在使用 Xcode 4,我的软件在 iPhone 4 和模拟器上运行良好,但是当我在 iPhone 2G 或 3G 等设备上测试它时,我在运行代码时立即出现此错误:

由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类与键 rootViewController 的键值编码不兼容。”

寻找解决问题的方法我构建了一个 hello world 程序,但在 iPhone 3gs 上不起作用......搜索我发现的解决方案:

// self.window.rootViewController = self.viewController; [self.window addSubview: [self.viewController 视图]];

像这样使用addsubview,程序应该运行良好......

好的,你好世界运行良好,但我的程序根本无法运行......

也许这是我应该改变的代码......(但我现在真的不......)

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
// Override point for customization after application launch.

//this and application should run on 3.1.3
if ([self.window respondsToSelector:@selector(setRootViewController:)])
    self.window.rootViewController = self.viewController;
else
    [self.window addSubview:self.viewController.view];

// Add registration for remote notifications
[[UIApplication sharedApplication] 
 registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

// Clear application badge when app launches
//application.applicationIconBadgeNumber = 0;

[self.window makeKeyAndVisible];

return YES;

}

我搜索了有关此问题的一些信息,但每次搜索时,我发现只更改了 self.window.rootViewController = self.viewController; 行

但不幸的是,帮助不大。

谢谢你们的耐心:)

编辑 :

我更改代码

if ([self.window respondsToSelector:@selector(setRootViewController:)])
    self.window.rootViewController = self.viewController;
else
    [self.window addSubview:self.viewController.view];

[self.window addSubview:self.viewController.view];

但错误仍然是一样的......

4

1 回答 1

1

UIWindowrootViewController在低于 4.0 的 iOS 版本中没有属性。因此,如果您想支持这些版本,则不能使用self.window.rootViewController = myViewController;,通常必须将控制器的视图添加到窗口中,即:[self.window addSubview:myViewController.view];

编辑:问题是你如何检查它是什么版本,因为 3.1.3 仍然可以响应 setRootViewController (内置但不是公共变量)。

于 2011-09-20T21:54:58.370 回答