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

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

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

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


当前回答

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

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

其他回答

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

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

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

如果你的应用程序用FingerTipWindow替换了主UIWindow(在投影仪上显示触摸),并且你已经好几年没有更新源代码了,你的替换对象可能不包括rootViewController属性(参见kgn的5/21/2013 mod at GitHub)

您可以设置窗口。在didFinishLaunchingWithOptions中的rootViewController直到牛回家,但是你的窗口不会报告一个“在应用程序启动结束时”,并且会在运行时抛出一个异常。更新你的资料来源。

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

我在一款面向iOS 5.1的iPad应用中遇到了这种情况。应用程序使用UITabBarController。我需要在标签栏控制器中创建一个新部分,所以我创建了一个新的视图控制器和xib。一旦我将新的视图控制器添加到选项卡栏控制器,我的屏幕上的控件都不再工作了,我得到了“期望有一个根视图控制器”日志。

新的xib中的顶级对象是UIWindow而不是UIView。当我将一个UIView放到XIB中,有了视图出口点,将所有子视图移动到新的UIView中,并删除了UIWindow实例,问题就解决了。