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

发布于 2024-10-27 03:06:45 字数 1776 浏览 0 评论 0原文

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

这与默认模板的主要区别在于我使用 RootViewControler 的表格视图来呈现目录结构。这一切都工作正常,直到我想从 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时,底部的第二个永远不会工作,因为它只在堆栈的较低级别中被调用。

I've been working on this for the past 11 hours and I think I've figured out what's going on but I can't figure out how to fix it. Basically I'm using the SplitView template for iPad. I have a RootViewController on the left and a DetailViewController on the right.

The main way this differs from the default template is that I'm using the RootViewControler's tableview to present a directory structure. That all works fine until I want to set a label on the DetailViewController from one of the deeper level in rootviewcontroller. I can set the label from the topmost rootviewcontroller but anything lower on the stack doesn't work.

I figure this is happening because every time you move down another level in the directory it pushes another instance of RootViewController onto the navigation stack and these new instances aren't connected to the DetailViewController. What I can't figure out is how to get the new instances to talk to the DetailViewController.

Here's the code:

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


}

Basically what should happen is if the user taps on a cell that is a folder it will push a new instance of RootViewController onto the navigation stack and display the contents of that directory in it. If the user clicks on a file it should set the detailItem to the name of that file (currently it's just placeholder code) which DetailViewController will then take and change a label on it's view to the file name.

The first detailViewController.detailItem at the top works but only if you're in the topmost RootViewController, the second at the bottom never works because it's only ever called in the lower levels on the stack.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀞厅☆埖开 2024-11-03 03:06:45

使用

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 是可行的。

Got it working using

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

Found the solution in this question: Problems accessing rootviewController of navcontroller iphone

I had tried using navigationController.topViewController but it would still return nil. However, using objectAtIndex:0 works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文