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

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

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

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


当前回答

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

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

在App Delegate方法上:

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

这工作。

其他回答

当我尝试改变加载的第一个视图控制器时,我有同样的错误

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

一开始我不知道错误到底从哪里来,所以我缩小了范围,找出了问题所在。结果是我试图在视图显示在屏幕上之前改变视图的显示。因此,解决方案是在视图控制器中移动这段代码,这给我带来了麻烦

- (void) viewDidLoad

to

- (void) viewDidAppear: (BOOL)动画

错误不再出现。我的问题是由制作一个UIAlertView显示引起的。

在你的情况下,我建议你检查在tabBarController的活动视图控制器的代码(因为它可能是一个问题在那个视图控制器)。 如果这不起作用,试着在nib文件中而不是在代码中设置起始设置——或者如果你想在代码中做,试着将代码移动到tabBarController的活动视图控制器的适当方法。

好运!

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

在我的例子中,关于实际窗口和didFinishLaunchingWithOptions:方法的一切都很好。

我的错误是我没有意识到applicationDidBecomeActive:在启动时运行,除了应用程序在后台运行后进入前台。

因此,在applicationDidBecomeActive中:我正在操作尚未完成所有设置的视图控制器(等待不同的线程响应等)。

一旦我把这个功能移到applicationDidBecomeActive之外,错误就消失了。

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

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

确保在应用程序委托中有这个函数。

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

确保didFinishLaunchingWithOptions返回YES。如果你碰巧删除了'return YES'行,这将导致错误。这个错误在故事板用户中可能特别常见。