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

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

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

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


当前回答

我也有这个问题。我让我的项目在xcode4.2.1中运行。我已经看了上面所有的评论,但没有人对我来说很酷。过了一会儿,我发现我注释了一段代码。

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

然后我取消了评论。我一切都好。 希望这对你们有帮助。

其他回答

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

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

我的第一个视图是MenuViewController,我添加:

MenuViewController *menuViewController = [[MenuViewController alloc]init];
self.window.rootViewController = menuViewController;

在App Delegate方法上:

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

这工作。

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

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

如果你使用MTStatusBarOverlay,你会得到这个错误。

MTStatusBarOverlay创建了一个额外的窗口([[UIApplication sharedApplication] windows),它没有根控制器。

这似乎没有引起问题。

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