Xcode 4 iphone 3.1.3 应用程序无法运行
我正在使用 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 ,程序应该运行良好...
好吧,hello world 运行良好,但我的程序根本不起作用...
也许这是我应该更改的代码...(但我现在真的不这样做。 .)
- (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];
但错误仍然相同......
i'm working with Xcode 4 and my software work well on iPhone 4 and simulator but when i test it on devices like iPhone 2G or 3Gs i have this error immediately when i run the code :
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key rootViewController.'
searching the way to solve the problem i build a hello world program and do not work on iPhone 3gs ... searching the solutions i found this :
// self.window.rootViewController = self.viewController;
[self.window addSubview: [self.viewController view]];
use addsubview like this and the program should run fine ...
Ok the hello world run well but my program does not work at all ...
maybe this is the code i should change ... (but i don't now really ..)
- (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;
}
i search some info about this problem but every time i search i found only to change the line
self.window.rootViewController = self.viewController;
but unfortunately don't help much.
thanks guys for you're patience :)
EDIT :
i change the code
if ([self.window respondsToSelector:@selector(setRootViewController:)])
self.window.rootViewController = self.viewController;
else
[self.window addSubview:self.viewController.view];
to
[self.window addSubview:self.viewController.view];
but the error is still the same ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在低于 4.0 的 iOS 版本中,
UIWindow
没有rootViewController
属性。因此如果你想支持这些版本,你不能使用 self.window.rootViewController = myViewController; ,你通常必须将控制器的视图添加到窗口,即:[self. window addSubview:myViewController.view];
编辑:问题是你如何检查它是什么版本,因为 3.1.3 仍然可以响应 setRootViewController (内置但不是公共变量)。
UIWindow
does not have arootViewController
property in iOS versions less than 4.0. Hence if you want to support these versions, you can't useself.window.rootViewController = myViewController;
, you generally have to add the controller's view to the window, i.e.:[self.window addSubview:myViewController.view];
Edit: the problem is how you are checking for what version it is, since 3.1.3 could have still responded to setRootViewController (built in but not public variable).