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

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

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

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


当前回答

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

self.window.rootViewController = self.tabBarController;

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

其他回答

如何为iOS5添加一个RootViewController

如果你的应用还没有使用RootViewController, 只需创建一个;)点击文件>新建>新建文件; 选择UIViewController子类 命名为RootViewController,取消用户界面With XIB(假设你已经有一个) 然后把这段代码放到你的AppDelegate:: didFinishLaunchingWithOptions中

rootViewController = [[RootViewController alloc] init];
window.rootViewController = rootViewController;

当然,你必须导入RootViewController.h并创建变量

这里有一篇关于RootViewController和AppDelegate的好文章,

在我的例子中,关于实际窗口和didFinishLaunchingWithOptions:方法的一切都很好。

我的错误是我没有意识到applicationDidBecomeActive:在启动时运行,除了应用程序在后台运行后进入前台。

因此,在applicationDidBecomeActive中:我正在操作尚未完成所有设置的视图控制器(等待不同的线程响应等)。

一旦我把这个功能移到applicationDidBecomeActive之外,错误就消失了。

确保在应用程序委托中有这个函数。

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

确保didFinishLaunchingWithOptions返回YES。如果你碰巧删除了'return YES'行,这将导致错误。这个错误在故事板用户中可能特别常见。

我从“空应用程序”模板开始,然后手动添加XIB时得到了这个。我通过设置Sunny建议的主Nib名称来解决这个问题。这个场景中缺少的步骤是删除

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

from

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

因为它会覆盖在Xib文件中创建的窗口实例。这是假设你已经创建了一个ViewController,并将它与XIB文件中的窗口和App Delegate连接起来。

我升级到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;
    }
}