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

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

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

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


当前回答

我从“空应用程序”模板开始,然后手动添加XIB时得到了这个。我通过设置Sunny建议的主Nib名称来解决这个问题。这个场景中缺少的步骤是删除

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

from

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

因为它会覆盖在Xib文件中创建的窗口实例。这是假设你已经创建了一个ViewController,并将它与XIB文件中的窗口和App Delegate连接起来。

其他回答

尝试将选项卡栏控制器的IBOutlet连接到接口生成器中的根视图,而不是

self.window.rootViewController = self.tabBarController;

但实际上我还没见过这样的错误。

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

我能够在xcode的摘要屏幕上设置初始视图控制器。

点击左边文件资源管理器中最上面的项目名称(它应该有一个小的蓝图图标)。在中间一栏中,点击“TARGETS”下面的项目名称(它旁边应该有一个小铅笔“a”图标)。在“iPhone / iPod部署信息”下寻找“主界面”。您应该能够从下拉菜单中选择一个选项。

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

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

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

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