2011-03-27 55 views
0

我一直在为此工作了过去的11个小时,我想我已经知道发生了什么,但我无法弄清楚如何解决它。基本上我使用iPad的SplitView模板。我在左边有一个RootViewController,在右边有一个DetailViewController。在导航堆栈中从rootviewcontroller的较低级别访问DetailViewController

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

我觉得这是因为每当你在目录中向下移动另一个级别时,它会将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中时,第二个位于底部的永远不会工作,因为它只在堆栈的较低层调用。

回答

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的作品。

相关问题