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

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

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

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


当前回答

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

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

In

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
...
UIStoryboard *stb = [UIStoryboard storyboardWithName:@"myStoryboard" bundle:nil];
self.window.rootViewController = [stb instantiateInitialViewController];
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;
}

选择你的“窗口”在你的Nib文件 在“属性检查器”中选择“启动时可见”

手动创建nib文件时会发生这种情况。 这个修复工作于常规的nib模式-不是故事板模式

当文件的主窗口所有者时也会出现此错误。Xib设置不正确。

文件的所有者是UIApplication->应用程序委托类的插入对象,窗口出口连接到窗口

验证你的STORYBOARD文件…如果你使用IB '电线'代码到你的UI..然后刚好删除了UI元素…然后意识到,哦,你需要删除代码中的引用(在我的情况下,一个名为AbortButton的UIButton)…然后过了一会儿,应用程序就不会启动了……

问题在“故事板”文件中。在xml编辑器中打开它,寻找孤立的“关系”,这些连接器指向不应该指向的地方……

eg:

<relationships> <关系种类=“出口” 名称=“中止按钮” 候选类=“UIButton”/>

在我的代码中已经不存在了…(然而我仍然有一个“连接器”链接在故事板文件)..

此外,我有(莫名其妙和无效的连接器从一个UIImageView或工具栏到我的主UIViewController。视图对象)…我想它也不喜欢……

底线-一个人不需要手动“编码控制器”的存在…STORYBOARD文件被破坏了。我修好了我的,宾果又完美地工作了。

顺便说一句,作为一个文本编码员。我越来越喜欢IB…踢屁股在MS Visual Studio的IDE…

在AppDelegate中替换

 [window addSubview:[someController view]];

to

  [self.window setRootViewController:someController];