2012-02-11 51 views
1

我正在嘗試使用表格視圖和導航控制器。這是我使用的代碼,如果我使用NIB文件。這段代碼將正常工作,如果我使用NIB文件,但我決定嘗試故事板 -推動故事板中的視圖

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

if (_webViewController == nil) { 
    self.webViewController = [[[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]] autorelease]; 
} 
RSSEntry *entry = [_allEntries objectAtIndex:indexPath.row]; 
_webViewController.entry = entry; 
[self.navigationController pushViewController:_webViewController animated:YES]; 
} 

我已經創造了故事板另一個場景和一切設置在那裏。當用戶點擊這個表格視圖中的單元格時,它們將被帶到該場景。這是我寫的代碼,但它是不正確的,我絕望地卡住:( -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
if ([segue.identifier isEqualToString:@"articleView"]){ 
    UINavigationController *navigationController = segue.destinationViewController; 
    WebViewController *controller = (WebViewController *)navigationController.topViewController; 
    controller.webView = self; 
} 

回答 - 答案2和3都是對

回答

2

我有一個非常類似的問題,3天前,與用戶對行選擇的披露附件和想要調用SEGUE 。對我而言:

  1. 在故事板中,從整個tableView控制器(不是單元格)創建segue「articleView」到下一個viewController。
  2. 添加以下方法給您的tableViewController:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { 
    [self performSegueWithIdentifier:@"articleView" sender:[self.tableView cellForRowAtIndexPath:indexPath]]; 
} 

只爲你我還用的tableView測試:didSelectRowAtIndexPath方法:和它的作品。

享受,

達明

+0

優秀的,它的工作。非常感謝:D – 2012-02-11 22:44:55

2

如果您的表視圖已經嵌入導航控制器(看起來是這樣,從你的第一個代碼片段),那麼你的目標視圖控制器r segue將是一個WebViewController,而不是導航控制器。

因此,你可以有你的prepareForSegue方法直接通過該項目跨越:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"articleView"]) 
    { 
     WebViewController *controller = (WebViewController *)segue.destinationViewController; 
     RSSEntry *entry = [_allEntries objectAtIndex:[self.tableView indexPathForSelectedRow].row]; 
     controller.entry = entry; 
    } 
}