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

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

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

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


当前回答

这发生在我身上是因为我无意中注释了:

[self.window makeKeyAndVisible];

from

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

其他回答

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

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

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

- (void) viewDidLoad

to

- (void) viewDidAppear: (BOOL)动画

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

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

好运!

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

我遇到了同样的问题,但我使用的是故事板

将我的故事板InitialViewController分配给我窗口的rootViewController。

In

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
...
UIStoryboard *stb = [UIStoryboard storyboardWithName:@"myStoryboard" bundle:nil];
self.window.rootViewController = [stb instantiateInitialViewController];
return YES;
}

这就解决了问题。

我能够在xcode的摘要屏幕上设置初始视图控制器。

点击左边文件资源管理器中最上面的项目名称(它应该有一个小的蓝图图标)。在中间一栏中,点击“TARGETS”下面的项目名称(它旁边应该有一个小铅笔“a”图标)。在“iPhone / iPod部署信息”下寻找“主界面”。您应该能够从下拉菜单中选择一个选项。

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