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

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

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

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


当前回答

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

其他回答

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

我升级到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;
    }
}

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

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

在我的例子中,关于实际窗口和didFinishLaunchingWithOptions:方法的一切都很好。

我的错误是我没有意识到applicationDidBecomeActive:在启动时运行,除了应用程序在后台运行后进入前台。

因此,在applicationDidBecomeActive中:我正在操作尚未完成所有设置的视图控制器(等待不同的线程响应等)。

一旦我把这个功能移到applicationDidBecomeActive之外,错误就消失了。

如何为iOS5添加一个RootViewController

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

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

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

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