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

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

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

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


当前回答

在AppDelegate中替换

 [window addSubview:[someController view]];

to

  [self.window setRootViewController:someController];

其他回答

我升级到iOS9,开始出现这个错误。我能够修复它,但添加以下代码到- (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow *window in windows) {
    if(window.rootViewController == nil){
        UIViewController* vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
        window.rootViewController = vc;
    }
}

ARC更改:

改变

@synthesize window;

而不是

@synthesize window=_window;

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

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

In

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

这就解决了问题。

当没有正确设置接口生成器时,会发生此问题。

确保你的App Delegate的窗口和viewController outlet被连接起来:

在你的主窗口。xib,按住control,点击App Delegate并拖动到Window对象。选择窗口。按住control并再次选择App委托,拖到你的根视图控制器并选择viewController。

在使用XCode 4.6.3和iOS 6.1将我的UI替换为Storyboard后收到相同的错误

通过清除AppDelegate中的didFinishLaucnhingWithOptions中的所有代码来解决这个问题

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