从导航堆栈中较低级别的 rootviewcontroller 访问 DetailViewController
过去 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
找到了这个问题的解决方案:Problems accessing rootviewController of navcontroller iphone
我曾尝试使用
navigationController.topViewController
,但它仍然会返回nil
。但是,使用objectAtIndex:0
是可行的。Got it working using
Found the solution in this question: Problems accessing rootviewController of navcontroller iphone
I had tried using
navigationController.topViewController
but it would still returnnil
. However, usingobjectAtIndex:0
works.