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

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

下面是我的应用程序: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中将一个旧的EAGLView示例项目迁移到新的GLKView示例项目中,没有一个解决方案适合我。最后我意识到我试图设置主GLKViewController的自我。视图指向Interface Builder中的嵌套GLKView。

当我指向自我时。视图回到根GLKView在接口生成器,我的应用程序能够启动没有一个错误消息(所以确保你的视图控制器的视图被设置为根视图)。

附注:如果你想让一个嵌套的GLKView工作,创建一个新的成员变量,如self。并将其连接拖到Interface Builder中嵌套的GLKView。然后确保拖动self。glkSubview的委托给文件的所有者。您必须手动调用[self。glkView:(glkView *)视图drawwinrect:(CGRect)rect"如果你有setNeedsDisplay关闭的glkView。

其他回答

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

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

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

- (void) viewDidLoad

to

- (void) viewDidAppear: (BOOL)动画

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

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

好运!

以上建议没有一个能解决我的问题。我的回答是:

Add:

window.rootViewController = navigationController;

后:

[window addSubview:navigationController.view];

在我的appdelegate中

- (void)applicationDidFinishLaunching:(UIApplication *)application {

在升级到Xcode 4.3后,我开始遇到同样的问题,而且只有在从头开始一个项目时(即创建一个空项目,然后创建一个UIViewController,然后创建一个单独的nib文件)。

在把我习惯的所有行,并确保我有正确的连接后,我一直得到这个错误,我试图通过视图控制器加载的nib文件(它被设置为rootController)从未在模拟器中显示。

我通过Xcode创建了一个视图模板,并将其与我的代码进行了比较,最终发现了问题!

Xcode 4.3 appears to add by default the method -(void)loadView; to the view controller implementation section. After carefully reading the comments inside it, it became clear what the problem was. The comment indicated to override loadView method if creating a view programmatically (and I'm paraphrasing), otherwise NOT to override loadView if using a nib. There was nothing else inside this method, so in affect I was overriding the method (and doing nothing) WHILE using a nib file, which gave the error.

解决方案是从实现部分完全删除loadView方法,或者通过添加[super loadView]来调用父方法。

如果使用NIB文件,则最好删除它,因为添加任何其他代码都会覆盖它。

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

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

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

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