2011-03-27 52 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的作品。