从导航堆栈中较低级别的rootviewcontroller访问DetailViewController

时间:2011-03-27 01:06:52

标签: iphone uitableview ipad cocoa-touch uinavigationcontroller

过去11个小时我一直在研究这个问题,我想我已经知道发生了什么,但我无法弄清楚如何解决这个问题。基本上我正在使用适用于iPad的SplitView模板。我左边有一个RootViewController,右边有一个DetailViewController。

这与默认模板的主要方式是我使用RootViewControler的tableview来呈现目录结构。一切正常,直到我想从rootviewcontroller的更深层次之一在DetailViewController上设置标签。我可以从最顶层的rootviewcontroller设置标签,但堆栈中较低的任何东西都不起作用。

我认为这种情况正在发生,因为每次你在目录中向下移动另一个级别时,它会将另一个RootViewController实例推送到导航堆栈,并且这些新实例没有连接到DetailViewController。我无法弄清楚的是如何让新实例与DetailViewController交谈。

以下是代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    detailViewController.detailItem = [NSString stringWithFormat:@"Row %d",indexPath.row];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

        NSString *type = [[dataSource objectAtIndex:indexPath.row] objectAtIndex:2];
        if ([type isEqualToString:@"tFolder"]) {
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            NSString *path = [[dataSource objectAtIndex:indexPath.row] objectAtIndex:0];
            UITableViewController *targetViewController = [[RootViewController alloc] initWithPath:path];
            [self.navigationController pushViewController:targetViewController animated:YES];
            [targetViewController release];
        } else if ([type isEqualToString:@"tFile"]) {
            NSLog(@"Setting title");
            detailViewController.detailItem = [NSString stringWithFormat:@"Row %d",indexPath.row];
        }


}

基本上应该发生的是,如果用户点击作为文件夹的单元格,它会将新的RootViewController实例推送到导航堆栈并在其中显示该目录的内容。如果用户点击一个文件,它应该将detailItem设置为该文件的名称(当前它只是占位符代码),然后DetailViewController将采用该文件并将其视图上的标签更改为文件名。

顶部的第一个detailViewController.detailItem工作,但只有当你在最顶层的RootViewController中时,底部的第二个才能工作,因为它只在堆栈的较低层调用。

1 个答案:

答案 0 :(得分:0)

使用

工作
RootViewController* topController = [self.navigationController.viewControllers objectAtIndex:0];
topController.detailViewController.detailItem = [NSString stringWithFormat:@"Row %d",indexPath.row];

在此问题中找到解决方案:Problems accessing rootviewController of navcontroller iphone

我曾尝试使用navigationController.topViewController,但仍会返回nil。但是,使用objectAtIndex:0可以正常工作。

相关问题