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

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

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

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


当前回答

以上这些方法对我都没用……发现我的appDelegate上的init方法有问题。如果您正在实现init方法,请确保正确执行

我有这个:

- (id)init {
    if (!self) {
        self = [super init];
        sharedInstance = self;
    }
    return sharedInstance;
}

然后改成了这样:

- (id)init {
    if (!self) {
        self = [super init];
    }
    sharedInstance = self;
    return sharedInstance;
}

哪里“sharedInstance”是一个指针到我的appDelegate单例

其他回答

这发生在我身上。通过编辑.plist文件解决。 指定Main nib文件基名。(应该是MainWindow.xib)。 希望这能有所帮助。

我得到这个错误(应用程序预计在应用程序启动结束时有一个根视图控制器),我正在以编程方式创建视图控制器。

通过确保我的根视图控制器中的loadView方法调用[super loadView]来解决这个问题。

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

在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
}

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

没有一个答案能完全解决我的问题。

我正在开发一个升级到ARC的旧iOS4项目,现在正在为iOS 7开发Xcode 5

我通读了所有这些文件,并开始检查我的配置和代码。

为我修复它的是添加

-(BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{
    // Maybe do something
    return YES;
}

除了有

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
}

我没有

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

在得到错误之前。