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

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

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

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


当前回答

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

其他回答

日志中也有同样的错误信息。我在应用中有一个UIAlertView弹出:didFinishLaunchingWithOptions。我通过延迟对alertView的调用来解决这个问题,让根视图控制器有时间完成加载。

在应用程序:didFinishLaunchingWithOptions:

[self performSelector:@selector(callPopUp) withObject:nil afterDelay:1.0];

1秒后调用:

- (void)callPopUp
{
    // call UIAlertView
}

移动setRootViewController:从didFinishLaunchingWithOptions:到awakeFromNib:解决了这个问题在我的空项目。

我升级到iOS9,开始出现这个错误。我能够修复它,但添加以下代码到- (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow *window in windows) {
    if(window.rootViewController == nil){
        UIViewController* vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
        window.rootViewController = vc;
    }
}

我通过以下方法解决了这个问题(上面的其他方法都没用):

从与“主界面”相关的下拉菜单中选择另一个条目,然后重新选择“主窗口”,然后重新构建。

我在一款面向iOS 5.1的iPad应用中遇到了这种情况。应用程序使用UITabBarController。我需要在标签栏控制器中创建一个新部分,所以我创建了一个新的视图控制器和xib。一旦我将新的视图控制器添加到选项卡栏控制器,我的屏幕上的控件都不再工作了,我得到了“期望有一个根视图控制器”日志。

新的xib中的顶级对象是UIWindow而不是UIView。当我将一个UIView放到XIB中,有了视图出口点,将所有子视图移动到新的UIView中,并删除了UIWindow实例,问题就解决了。