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

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

下面是我的应用程序: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中替换

 [window addSubview:[someController view]];

to

  [self.window setRootViewController:someController];

其他回答

我能够在xcode的摘要屏幕上设置初始视图控制器。

点击左边文件资源管理器中最上面的项目名称(它应该有一个小的蓝图图标)。在中间一栏中,点击“TARGETS”下面的项目名称(它旁边应该有一个小铅笔“a”图标)。在“iPhone / iPod部署信息”下寻找“主界面”。您应该能够从下拉菜单中选择一个选项。

在AppDelegate中替换

 [window addSubview:[someController view]];

to

  [self.window setRootViewController:someController];

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

-(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;
}

我希望这能有所帮助。

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

我最近在用ios5 sdk构建一个项目时遇到了同样的问题。 起初,它是构建和运行正常的,但在那之后,错误出现了。 对我来说,解决办法相当简单。 所缺少的是,我的应用程序目标的摘要选项卡中的主界面属性被擦除了。所以我需要重新设置。

如果这不是重点,如果tabBarController仍然是nil,你总是可以通过编程方式创建你的窗口和根控制器。 作为备用方案,我将以下代码添加到我的项目中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
    if (!window && !navigationController) {
        NSLog(@"Window and navigation controller not loaded from nib. Will be created programatically.");
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        UIViewController *viewController1, *viewController2;
        viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil] autorelease];
        viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil] autorelease];

        self.tabBarController = [[[UITabBarController alloc] init] autorelease];
        self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
        self.window.rootViewController = self.tabBarController;

    }
    else {
        [window addSubview:[tabBarController view]];
    }
    [self.window makeKeyAndVisible];
    return YES;
}

只有当sho的解决方案也被实现时,这才会起作用。