我在控制台得到以下错误:

应用程序在启动结束时应该有一个根视图控制器

下面是我的应用程序:didFinishLaunchWithOptions方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Set Background Color/Pattern
    self.window.backgroundColor = [UIColor blackColor];
    self.tabBarController.tabBar.backgroundColor = [UIColor clearColor];
    //self.window.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"testbg.png"]];

    // Set StatusBar Color
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];

    // Add the tab bar controller's current view as a subview of the window
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

在Interface Builder中,UITabBarController的委托被连接到App委托。

有人知道怎么解决这个问题吗?


当前回答

我最近在用ios5 sdk构建一个项目时遇到了同样的问题。 起初,它是构建和运行正常的,但在那之后,错误出现了。 对我来说,解决办法相当简单。 所缺少的是,我的应用程序目标的摘要选项卡中的主界面属性被擦除了。所以我需要重新设置。

如果这不是重点,如果tabBarController仍然是nil,你总是可以通过编程方式创建你的窗口和根控制器。 作为备用方案,我将以下代码添加到我的项目中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
    if (!window && !navigationController) {
        NSLog(@"Window and navigation controller not loaded from nib. Will be created programatically.");
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        UIViewController *viewController1, *viewController2;
        viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil] autorelease];
        viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil] autorelease];

        self.tabBarController = [[[UITabBarController alloc] init] autorelease];
        self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
        self.window.rootViewController = self.tabBarController;

    }
    else {
        [window addSubview:[tabBarController view]];
    }
    [self.window makeKeyAndVisible];
    return YES;
}

只有当sho的解决方案也被实现时,这才会起作用。

其他回答

我突然意识到这个错误。那么,是什么引起的呢?

结果是我在IB中将文件所有者附加到一个新的小ImageView,我将它拖到视图上。在.h文件中,我没有将它称为IBOutlet,因此当我按ctrl拖动到它时,新的Imageview没有被列为可能的连接。小黑框中显示的唯一可能性是视图。我一定是无意中点击了。我做了一些改变,然后运行程序,得到根控制器错误。修复是重新连接文件所有者到xib - IB屏幕的底部视图。

听起来像self。tabBarController返回nil。tabBarController可能没有在接口生成器中连接起来。在Interface Builder中将tabBarController的IBOutlet设置为tabBarController。

在升级到Xcode 4.3后,我开始遇到同样的问题,而且只有在从头开始一个项目时(即创建一个空项目,然后创建一个UIViewController,然后创建一个单独的nib文件)。

在把我习惯的所有行,并确保我有正确的连接后,我一直得到这个错误,我试图通过视图控制器加载的nib文件(它被设置为rootController)从未在模拟器中显示。

我通过Xcode创建了一个视图模板,并将其与我的代码进行了比较,最终发现了问题!

Xcode 4.3 appears to add by default the method -(void)loadView; to the view controller implementation section. After carefully reading the comments inside it, it became clear what the problem was. The comment indicated to override loadView method if creating a view programmatically (and I'm paraphrasing), otherwise NOT to override loadView if using a nib. There was nothing else inside this method, so in affect I was overriding the method (and doing nothing) WHILE using a nib file, which gave the error.

解决方案是从实现部分完全删除loadView方法,或者通过添加[super loadView]来调用父方法。

如果使用NIB文件,则最好删除它,因为添加任何其他代码都会覆盖它。

我也有同样的问题。检查你的主机。最后一个参数应该设置为实现UIApplicationDelegate协议的类的名称。

retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");

在ios5.0左右有一个微小的变化,要求你有一个根视图控制器。如果您的代码基于较旧的示例代码,例如GLES2Sample,那么在这些代码示例中没有创建根视图控制器。

为了修复(例如,GLES2Sample),在applicationDidFinishLaunching中,我创建了一个根视图控制器,并将我的glView附加到它。

- (void) applicationDidFinishLaunching:(UIApplication *)application
{
  // To make the 'Application windows are expected
  // to have a root view controller
  // at the end of application launch' warning go away,
  // you should have a rootviewcontroller,
  // but this app doesn't have one at all.
  window.rootViewController = [[UIViewController alloc] init];  // MAKE ONE
  window.rootViewController.view = glView; // MUST SET THIS UP OTHERWISE
  // THE ROOTVIEWCONTROLLER SEEMS TO INTERCEPT TOUCH EVENTS
}

这使得警告消失,并没有真正影响你的应用程序。