Xcode 4.5黑屏或异常:'视图一次只能与一个视图控制器关联!'

时间:2012-10-02 03:37:05

标签: objective-c xcode cocos2d-iphone

我有一个现有的iPhone应用程序,在升级到Xcode 4.5之前在Xcode 4.0中运行良好。升级后,现在我在iPhone / iPad 4.3模拟器中运行时会出现黑屏,并在运行iPhone / iPad 6.0模拟器时出现以下异常。

Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'A view can only be associated with at most one view controller at a time! View <CCGLView: 0x8c7f380; frame = (0 0; 320 480); layer = <CAEAGLLayer: 0x8c7f670>> is associated with <CCDirectorDisplayLink = 0x994c7f0 | Size: 320 x 480, view = <CCGLView: 0x8c7f380; frame = (0 0; 320 480); layer = <CAEAGLLayer: 0x8c7f670>>>. Clear this association before associating this view with <RootViewController: 0x8c7ef00>.'

该应用程序正在使用Cocos2D 2.0,并且基于在线发现的教程非常简单。没有XIB文件。一切都是以编程方式完成的。

这些是导致6.0模拟器中的异常的行(来自IOS6TestAppDelegate.m):

viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
[enter link description here][1]viewController.wantsFullScreenLayout = YES;

如果这些行被注释掉,那么6.0模拟器也会显示黑屏,而不是常规菜单屏幕。

我在网上搜索了解决方案并尝试了很多方法,但一直无法取得进展。我对iPhone编程相当新手(虽然对于一般的编程并不陌生)而且我真的很挣扎。我真的很感激任何帮助。

1 个答案:

答案 0 :(得分:3)

首先,删除

viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;

然后替换

[window setRootViewController:viewController];

使用

if( ! [director enableRetinaDisplay:YES] )
    CCLOG(@"Retina Display Not supported");

// Create a Navigation Controller with the Director
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:director];
navController.navigationBarHidden = YES;
NSString *reqSysVer = @"6.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
{
    [window setRootViewController:navController];
} else
{
    [window addSubview: navController.view];
}

应该这样做!