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

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

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

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


当前回答

我最近在用ios5 sdk构建一个项目时遇到了同样的问题。 起初,它是构建和运行正常的,但在那之后,错误出现了。 对我来说,解决办法相当简单。 所缺少的是,我的应用程序目标的摘要选项卡中的主界面属性被擦除了。所以我需要重新设置。

如果这不是重点,如果tabBarController仍然是nil,你总是可以通过编程方式创建你的窗口和根控制器。 作为备用方案,我将以下代码添加到我的项目中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
    if (!window && !navigationController) {
        NSLog(@"Window and navigation controller not loaded from nib. Will be created programatically.");
        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        UIViewController *viewController1, *viewController2;
        viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil] autorelease];
        viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil] autorelease];

        self.tabBarController = [[[UITabBarController alloc] init] autorelease];
        self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
        self.window.rootViewController = self.tabBarController;

    }
    else {
        [window addSubview:[tabBarController view]];
    }
    [self.window makeKeyAndVisible];
    return YES;
}

只有当sho的解决方案也被实现时,这才会起作用。

其他回答

我的第一个视图是MenuViewController,我添加:

MenuViewController *menuViewController = [[MenuViewController alloc]init];
self.window.rootViewController = menuViewController;

在App Delegate方法上:

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

这工作。

在“sho”答案的顶部,这是正确的(UIApplicationMain的第四个参数应该是主控制器的名称),我添加了一些注释。

我最近改变了我的一个应用程序的“模型”使用主窗口。Xib以编程方式构造窗口。该应用程序使用了一个较旧的模板,自动创建了主窗口。因为我想为iPhone 5支持不同的控制器视图XIB,所以在创建App Delegate时,通过编程方式选择正确的XIB更容易。我删除了主窗口。Xib项目以及。

问题是,我忘记在UIApplication main中填充第四个参数,我忘记在项目摘要中从“主界面”中删除主窗口。

这导致了一个大问题:它在开发设备上呈现了无害的警告“应用程序预计会……”,但当它进入App Store时,它在消费者手机上崩溃了,因为MainWindow不再在捆绑包中!我不得不请求对漏洞修复进行快速审查。

另一个症状是,当设置被更改,应用程序被放在前台时,有时会出现一个白色块,比如一个空白的UIView。在iPhone 5中,很明显它是320x480块。也许丢失的主窗口是在开发模式下创建的,使用旧的大小。当第一个崩溃报告到达收件箱时,我刚刚发现了这个错误。

从app Store安装而不是从XCode安装,结果显示应用确实崩溃了,主窗口问题也在日志中暴露出来,所以我可以看出这不是设备+IOS版本的特殊组合。

你试过设置委托吗?

self.rootController.delegate = self;

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

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

日志中也有同样的错误信息。我在应用中有一个UIAlertView弹出:didFinishLaunchingWithOptions。我通过延迟对alertView的调用来解决这个问题,让根视图控制器有时间完成加载。

在应用程序:didFinishLaunchingWithOptions:

[self performSelector:@selector(callPopUp) withObject:nil afterDelay:1.0];

1秒后调用:

- (void)callPopUp
{
    // call UIAlertView
}