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

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

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

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


当前回答

我也有这个问题。事实证明,当我从左边的对象列表中删除应用程序委托时,我删除了应用程序委托,窗口和TabBarController的连接:)

其他回答

OrdoDei给出了一个正确而有价值的答案。我添加这个答案只是为了给出一个didFinishLaunchingWithOptions方法的例子,该方法使用了他的答案以及其他关于导航控制器的评论。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    // Instantiate the main menu view controller (UITableView with menu items).
    // Pass that view controller to the nav controller as the root of the nav stack.
    // This nav stack drives our *entire* app.
    UIViewController *viewController = [[XMMainMenuTableViewController alloc] init];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

    // Instantiate the app's window. Then get the nav controller's view into that window, and onto the screen.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // [self.window addSubview:self.navigationController.view];
    // The disabled line above was replaced by line below. Fixed Apple's complaint in log: Application windows are expected to have a root view controller at the end of application launch
    [self.window setRootViewController:self.navigationController];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

确保你的“Is Initial View Controller”为你的第一个场景正确设置。

这就是导致错误的原因。

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

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

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

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

当文件的主窗口所有者时也会出现此错误。Xib设置不正确。

文件的所有者是UIApplication->应用程序委托类的插入对象,窗口出口连接到窗口