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

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

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

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


当前回答

我在使用自定义ServiceLocator使用CoreData时得到了这样的错误

let context: NSManagedObjectContext = try self.dependencies.resolve()

//solution
let context: NSManagedObjectContext? = try? self.dependencies.resolve()

其他回答

在ios5.0左右有一个微小的变化,要求你有一个根视图控制器。如果您的代码基于较旧的示例代码,例如GLES2Sample,那么在这些代码示例中没有创建根视图控制器。

为了修复(例如,GLES2Sample),在applicationDidFinishLaunching中,我创建了一个根视图控制器,并将我的glView附加到它。

- (void) applicationDidFinishLaunching:(UIApplication *)application
{
  // To make the 'Application windows are expected
  // to have a root view controller
  // at the end of application launch' warning go away,
  // you should have a rootviewcontroller,
  // but this app doesn't have one at all.
  window.rootViewController = [[UIViewController alloc] init];  // MAKE ONE
  window.rootViewController.view = glView; // MUST SET THIS UP OTHERWISE
  // THE ROOTVIEWCONTROLLER SEEMS TO INTERCEPT TOUCH EVENTS
}

这使得警告消失,并没有真正影响你的应用程序。

虽然很多答案看起来都是正确的,但没有一个能帮我解决问题。我正在尝试使用空的应用程序模板,并试图为了理解而直接加载到.xib文件(就像旧的窗口模板)。苹果似乎在运行NSLog消息。

我使用的是Xcode 4.3,似乎没有任何东西可以消除这条消息,我想知道为什么。最后,我决定在Xcode 4.5(预览版/iPhone 6.0版本)中看看会发生什么,这条消息不再存在。在移动。

我收到了同样的错误信息,因为我调用了警报

- (void)applicationDidBecomeActive:(UIApplication *)application 

而不是

- (void)applicationWillEnterForeground:(UIApplication *)application

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

[self.window makeKeyAndVisible];

from

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

以上这些方法对我都没用……发现我的appDelegate上的init方法有问题。如果您正在实现init方法,请确保正确执行

我有这个:

- (id)init {
    if (!self) {
        self = [super init];
        sharedInstance = self;
    }
    return sharedInstance;
}

然后改成了这样:

- (id)init {
    if (!self) {
        self = [super init];
    }
    sharedInstance = self;
    return sharedInstance;
}

哪里“sharedInstance”是一个指针到我的appDelegate单例