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

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

下面是我的应用程序: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委托。

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


当前回答

我也有这个错误,但不像之前列出的任何答案,我的是因为我在我新生成的控制器(xcode 4.2, ios5)中取消了“loadView”方法的注释。

 //Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView  
{
}

它甚至告诉我,该方法是为创建视图编程,但我错过了它,因为它看起来如此类似于其他方法,如viewDidLoad,我通常使用我没有抓住它。

要解决这个问题,只需删除该方法,如果你不是通过编程方式创建视图层次结构,也就是使用nib或storyboard。

其他回答

在升级到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文件,则最好删除它,因为添加任何其他代码都会覆盖它。

我在一款面向iOS 5.1的iPad应用中遇到了这种情况。应用程序使用UITabBarController。我需要在标签栏控制器中创建一个新部分,所以我创建了一个新的视图控制器和xib。一旦我将新的视图控制器添加到选项卡栏控制器,我的屏幕上的控件都不再工作了,我得到了“期望有一个根视图控制器”日志。

新的xib中的顶级对象是UIWindow而不是UIView。当我将一个UIView放到XIB中,有了视图出口点,将所有子视图移动到新的UIView中,并删除了UIWindow实例,问题就解决了。

尝试将选项卡栏控制器的IBOutlet连接到接口生成器中的根视图,而不是

self.window.rootViewController = self.tabBarController;

但实际上我还没见过这样的错误。

如果你的应用程序用FingerTipWindow替换了主UIWindow(在投影仪上显示触摸),并且你已经好几年没有更新源代码了,你的替换对象可能不包括rootViewController属性(参见kgn的5/21/2013 mod at GitHub)

您可以设置窗口。在didFinishLaunchingWithOptions中的rootViewController直到牛回家,但是你的窗口不会报告一个“在应用程序启动结束时”,并且会在运行时抛出一个异常。更新你的资料来源。

确保你的“Is Initial View Controller”为你的第一个场景正确设置。

这就是导致错误的原因。