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

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

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

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


当前回答

我遇到了同样的问题,但我使用的是故事板

将我的故事板InitialViewController分配给我窗口的rootViewController。

In

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
...
UIStoryboard *stb = [UIStoryboard storyboardWithName:@"myStoryboard" bundle:nil];
self.window.rootViewController = [stb instantiateInitialViewController];
return YES;
}

这就解决了问题。

其他回答

我也有同样的问题。如果你像我一样“从零开始”构建一个基于窗口的应用程序,你需要做以下工作:(注意,这些是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检查器。确保选中“发射时可见”。

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

这个Swift 2解决方案对我来说很有效:

在AppDelegate -> didFinishLaunchingWithOptions中插入下面的代码

self.window!.rootViewController = storyboard.instantiateViewControllerWithIdentifier("YourRootViewController") as? YourRootViewControllerClass

我升级到iOS9,开始出现这个错误。我能够修复它,但添加以下代码到- (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow *window in windows) {
    if(window.rootViewController == nil){
        UIViewController* vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
        window.rootViewController = vc;
    }
}

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

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

我遇到了同样的问题,但我使用的是故事板

将我的故事板InitialViewController分配给我窗口的rootViewController。

In

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
...
UIStoryboard *stb = [UIStoryboard storyboardWithName:@"myStoryboard" bundle:nil];
self.window.rootViewController = [stb instantiateInitialViewController];
return YES;
}

这就解决了问题。