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 …
UIWindowdoes not have arootViewControllerproperty 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).