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

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

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

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


当前回答

如何为iOS5添加一个RootViewController

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

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

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

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

其他回答

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

self.window.rootViewController = self.tabBarController;

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

听起来像self。tabBarController返回nil。tabBarController可能没有在接口生成器中连接起来。在Interface Builder中将tabBarController的IBOutlet设置为tabBarController。

我也有这个错误,但没有答案已经列出是解决我的问题。 在我的例子中,日志显示是因为我在另一个子线程中分配了应用程序根视图控制器。

-(BOOL) application:(UIApplication*) application didFinishLaunchingWithOptions:(NSDictionary*) launchOptions
{
    ...
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        ...
        dispatch_async(dispatch_get_main_queue(), ^{
            ...
            [self updateTabBarTitles];
            self.window.rootViewController = self.tabBarController;
            ...
        });
    });

    [self.window makeKeyAndVisible];
    return YES;
}

通过将rootViewController分配移动到函数的末尾—就在调用makeKeyAndVisible之前:—将导致日志消息不再显示。

{
    ...
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

我希望这能有所帮助。

我通过以下方法解决了这个问题(上面的其他方法都没用):

从与“主界面”相关的下拉菜单中选择另一个条目,然后重新选择“主窗口”,然后重新构建。

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

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