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

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

下面是我的应用程序: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 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的解决方案也被实现时,这才会起作用。

其他回答

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

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

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

没有一个答案能完全解决我的问题。

我正在开发一个升级到ARC的旧iOS4项目,现在正在为iOS 7开发Xcode 5

我通读了所有这些文件,并开始检查我的配置和代码。

为我修复它的是添加

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

除了有

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
}

我没有

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

在得到错误之前。

虽然很多答案看起来都是正确的,但没有一个能帮我解决问题。我正在尝试使用空的应用程序模板,并试图为了理解而直接加载到.xib文件(就像旧的窗口模板)。苹果似乎在运行NSLog消息。

我使用的是Xcode 4.3,似乎没有任何东西可以消除这条消息,我想知道为什么。最后,我决定在Xcode 4.5(预览版/iPhone 6.0版本)中看看会发生什么,这条消息不再存在。在移动。

我也有这个错误,但不像之前列出的任何答案,我的是因为我在我新生成的控制器(xcode 4.2, ios5)中取消了“loadView”方法的注释。

 //Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView  
{
}

它甚至告诉我,该方法是为创建视图编程,但我错过了它,因为它看起来如此类似于其他方法,如viewDidLoad,我通常使用我没有抓住它。

要解决这个问题,只需删除该方法,如果你不是通过编程方式创建视图层次结构,也就是使用nib或storyboard。

我最近在用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的解决方案也被实现时,这才会起作用。