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

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

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

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


当前回答

在使用XCode 4.6.3和iOS 6.1将我的UI替换为Storyboard后收到相同的错误

通过清除AppDelegate中的didFinishLaucnhingWithOptions中的所有代码来解决这个问题

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

其他回答

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

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

这个Swift 2解决方案对我来说很有效:

在AppDelegate -> didFinishLaunchingWithOptions中插入下面的代码

self.window!.rootViewController = storyboard.instantiateViewControllerWithIdentifier("YourRootViewController") as? YourRootViewControllerClass

如何为iOS5添加一个RootViewController

如果你的应用还没有使用RootViewController, 只需创建一个;)点击文件>新建>新建文件; 选择UIViewController子类 命名为RootViewController,取消用户界面With XIB(假设你已经有一个) 然后把这段代码放到你的AppDelegate:: didFinishLaunchingWithOptions中

rootViewController = [[RootViewController alloc] init];
window.rootViewController = rootViewController;

当然,你必须导入RootViewController.h并创建变量

这里有一篇关于RootViewController和AppDelegate的好文章,

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

这就是导致错误的原因。

为了补充迈克弗林的答案,因为升级到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;
    }
}