Accessing DetailViewController from lower levels of rootviewcontroller in a navigation stack
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 开发者_如何学Gothe 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.
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.
精彩评论