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

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

下面是我的应用程序: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文件(就像旧的窗口模板)。苹果似乎在运行NSLog消息。

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

其他回答

我突然意识到这个错误。那么,是什么引起的呢?

结果是我在IB中将文件所有者附加到一个新的小ImageView,我将它拖到视图上。在.h文件中,我没有将它称为IBOutlet,因此当我按ctrl拖动到它时,新的Imageview没有被列为可能的连接。小黑框中显示的唯一可能性是视图。我一定是无意中点击了。我做了一些改变,然后运行程序,得到根控制器错误。修复是重新连接文件所有者到xib - IB屏幕的底部视图。

如何为iOS5添加一个RootViewController

如果你的应用还没有使用RootViewController, 只需创建一个;)点击文件>新建>新建文件; 选择UIViewController子类 命名为RootViewController,取消用户界面With XIB(假设你已经有一个) 然后把这段代码放到你的AppDelegate:: didFinishLaunchingWithOptions中

rootViewController = [[RootViewController alloc] init];
window.rootViewController = rootViewController;

当然,你必须导入RootViewController.h并创建变量

这里有一篇关于RootViewController和AppDelegate的好文章,

这发生在我身上。通过编辑.plist文件解决。 指定Main nib文件基名。(应该是MainWindow.xib)。 希望这能有所帮助。

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

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

from

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

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

你试过设置委托吗?

self.rootController.delegate = self;

在applicationDidFinishLaunchingWithOptions吗?这对我很有效,尽管我不确定为什么。