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

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

下面是我的应用程序: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];

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

其他回答

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

self.window.rootViewController = self.tabBarController;

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

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

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

为了补充迈克弗林的答案,因为升级到Xcode 7和在iOS 9设备上运行我的应用程序,我添加了这个到我的(BOOL)应用程序:(UIApplication *)应用didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

// Hide any window that isn't the main window
NSArray *windows = [[UIApplication sharedApplication] windows];
for (UIWindow *window in windows) {
    if (window != self.window) {
        window.hidden = YES;
    }
}

我突然意识到这个错误。那么,是什么引起的呢?

结果是我在IB中将文件所有者附加到一个新的小ImageView,我将它拖到视图上。在.h文件中,我没有将它称为IBOutlet,因此当我按ctrl拖动到它时,新的Imageview没有被列为可能的连接。小黑框中显示的唯一可能性是视图。我一定是无意中点击了。我做了一些改变,然后运行程序,得到根控制器错误。修复是重新连接文件所有者到xib - IB屏幕的底部视图。