我在iOS应用程序上工作两天来解决轮换问题,但没有成功。我在互联网上到处搜索任何解决方案,但找不到任何可以修复我的应用程序轮换的内容。
我已将iOS 6中引入的所有新功能添加到我的AppDelegate类中。
这是我在AppDelegate中的代码。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
HomeViewController *controller;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
controller = [[HomeViewController alloc] initWithNibName: @"iPadHomePage" bundle: nil];
}
else {
controller = [[HomeViewController alloc] initWithNibName: @"HomePage" bundle: nil];
}
NSMutableArray *controllers = [[NSMutableArray alloc] init];
[controllers addObject:controller];
navController.viewControllers = controllers;
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
当我谷歌时,我发现我不应该使用[self.window addSubview:tabBarController.view]
因为这不适用于iOS 6,所以我在AppDelegate类中用[self.window setRootViewController:tabBarController]
替换它,但后来我收到错误在主要班级。
由于未捕获的异常'NSUnknownKeyException'而终止应用, 原因:'[setValue:forUndefinedKey:]: 此类不是密钥labelName
的密钥值编码兼容
我错过了什么?
答案 0 :(得分:2)
那次崩溃与轮换无关。在您的笔尖中,您已将一个元素(可能是标签)连接到代码中不再存在的插座。您可能删除了名为“labelName”的属性或实例变量,但忘记删除界面构建器中的连接。
答案 1 :(得分:0)
此方法应添加到tabbarController.m:
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
并且此错误“因未捕获的异常而终止应用程序'NSUnknownKeyException',原因:'[setValue:forUndefinedKey:]:此类不符合键值labelName的密钥值”,我猜是你的tabbarController.view` s xib链接tabbarcontroller中的nonxist IBOutlet。
希望这可以帮到你
答案 2 :(得分:0)
最后我发现了这个问题。
用[self.window addSubview:tabBarController.view]
替换代码[self.window setRootViewController:tabBarController]
是不够的,订单也很重要。我将[self.window setRootViewController:tabBarController]; [self.window makeKeyAndVisible]
移到return YES;
之前的末尾,现在一切正常。 IBOutlet与标签之间的联系没有问题,.....
正确的代码是:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
HomeViewController *controller;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
controller = [[HomeViewController alloc] initWithNibName: @"iPadHomePage" bundle: nil];
}
else {
controller = [[HomeViewController alloc] initWithNibName: @"HomePage" bundle: nil];
}
NSMutableArray *controllers = [[NSMutableArray alloc] init];
[controllers addObject:controller];
navController.viewControllers = controllers;
//this is the fix, by moving this 2 lines here
[self.window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
return YES;
}