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

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

下面是我的应用程序: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()

其他回答

在AppDelegate中替换

 [window addSubview:[someController view]];

to

  [self.window setRootViewController:someController];

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

[self.window makeKeyAndVisible];

from

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

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

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

In

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

这就解决了问题。

以上这些方法对我都没用……发现我的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单例

我也有同样的问题。检查你的主机。最后一个参数应该设置为实现UIApplicationDelegate协议的类的名称。

retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");