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

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

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

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


当前回答

日志中也有同样的错误信息。我在应用中有一个UIAlertView弹出:didFinishLaunchingWithOptions。我通过延迟对alertView的调用来解决这个问题,让根视图控制器有时间完成加载。

在应用程序:didFinishLaunchingWithOptions:

[self performSelector:@selector(callPopUp) withObject:nil afterDelay:1.0];

1秒后调用:

- (void)callPopUp
{
    // call UIAlertView
}

其他回答

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

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

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

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

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

在AppDelegate中替换

 [window addSubview:[someController view]];

to

  [self.window setRootViewController:someController];

我也有同样的问题。如果你像我一样“从零开始”构建一个基于窗口的应用程序,你需要做以下工作:(注意,这些是Xcode 4.2的步骤。)

0. 确保你的应用委托符合UIApplicationDelegate协议。

例如,假设我们的委托名为MyAppDelegate。在MyAppDelegate.h中,我们应该有这样的东西:

@interface MyAppDelegate : 
    NSObject <UIApplicationDelegate> // etc...

1. 在main.m中指定应用程序委托

例如,

#import "MyAppDelegate.h"

int main(int argc, char *argv[])
{
  @autoreleasepool {
    return UIApplicationMain(argc, argv,
      nil, NSStringFromClass([MyAppDelegate class]));
  }
}

2. 创建一个主窗口界面文件。

为此,右键单击项目并选择新建文件。从那里,选择窗口从iOS ->用户界面部分。

将文件添加到项目后,转到项目的摘要(左键单击项目;点击总结。)在iPhone/iPod部署信息下(如果你喜欢的话,还有相应的iPad部分),在“主界面”组合框中选择你的新界面文件。

3.在接口编辑器中将它们全部连接起来

在文件列表中选择接口文件以调出接口编辑器。

确保Utilities窗格是打开的。

通过从“实用工具”窗格中的“对象”列表中拖动一个对象到窗口对象的上方或下方的空间中添加一个新对象。选择对象。单击Utilities窗格中的Identity检查器。将Class更改为应用程序的委托(本例中为MyAppDelegate)。

调出MyAppDelegate的连接检查器。将窗口出口连接到接口文件中已经存在的窗口。

单击左边的File's Owner,然后单击Utilities窗格中的Identity检查器。将类更改为UIApplication

为File's Owner调出连接检查器。将委托出口连接到MyAppDelegate对象。

4. 最后,也是非常重要的一点,单击接口文件中的Window对象。打开Attributes检查器。确保选中“发射时可见”。

这就是我要让它为我工作所做的一切。好运!

我收到了同样的错误信息,因为我调用了警报

- (void)applicationDidBecomeActive:(UIApplication *)application 

而不是

- (void)applicationWillEnterForeground:(UIApplication *)application