3

在一個簡單的測試應用程序中,我試圖將名爲「thisArray」的數組對象從MasterViewController傳遞到DetailViewController中名爲「passedData」的字符串。我正在使用故事板,並且UIViewController嵌入在導航控制器中。使用prepareForSegue方法,我順利地通過了數據UIViewController之間:在視圖控制器之間傳遞數據DidSelectRowsAtIndexPath Storyboards

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"pushData"]) { 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     DetailViewController *destViewController = segue.destinationViewController; 
     destViewController.passedData = [thisArray objectAtIndex:indexPath.row]; 
    } 
} 

現在對於一些原因,我想用didSelectRowsAtIndexPath而不是prepareForSegue。我用過這個:

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSMutableString *object = thisArray[indexPath.row]; 
    detailViewController.passedData = object; 
} 

但它沒有工作。我用過以下內容:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; 

    NSMutableString *object = thisArray[indexPath.row]; 
    detailViewController.passedData = object; 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
} 

但它也沒有工作。

問題

1)如何正確書寫didSelectRowsAtIndexPath更換prepareForSegue

2)如果我使用didSelectRowsAtIndexPath,做我需要刪除的故事板UIViewController之間的連接賽格瑞?

3)如果視圖控制器之間真的沒有segue連接怎麼辦?我怎麼能仍然使用didSelectRowAtIndexPath來傳遞它們之間的數據?

謝謝!

更新:根據答案,我收到的意見,我已經寫了以下內容:

首先,我已經去掉了控制器之間的SEGUE連接,設置故事板ID DetailViewController,類的名稱也是DetailViewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UIStoryboard* sb = [UIStoryboard storyboardWithName:@"DetailViewController" 
                bundle:nil]; 
    UIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"DetailViewController"]; 

    NSMutableString *object = thisArray[indexPath.row]; 
    detailViewController.passedData = object; 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
} 

但它越來越與以下錯誤崩潰:

*** Terminating app due to uncaught exception NSInvalidArgumentException , reason: 'Could not find a storyboard named DetailViewController in bundle

+0

見dasblinkenlight的編輯,並更改更新的代碼給他。它應該照顧你正在嘗試做的事情!:) – lnafziger 2013-05-05 02:21:19

+0

是的,它完成了。謝謝! – AJ112 2013-05-05 04:06:53

回答

10

你的第二次嘗試是接近直角的。唯一不正確的是你從故事板實例化視圖控制器的方式:你使用initWithNibNamed:,這是你使用NIB而不是故事板。對於故事板,您需要:

UIStoryboard* sb = [UIStoryboard storyboardWithName:@"theStoryboardId" 
              bundle:nil]; 
// If this code is inside a method called by a controller that is itself instantiated 
// from a storyboard, you can replace the above line with this.storyboard 
UIViewController* detailViewController = [sb instantiateViewControllerWithIdentifier:@"DetailViewController"]; 

有關詳細信息,請參閱this question

一旦你進行了替換,你的代碼應該按預期工作。

編輯::這裏是如何你的方法看起來:下面

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UIViewController* detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"]; 
    NSMutableString *object = thisArray[indexPath.row]; 
    detailViewController.passedData = object; 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
} 
+2

顯然,如果轉到另一個故事板,該語法非常有用。但如果相同的故事板,只需使用'self.storyboard',例如'UIViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@「DetailViewController」];' – Rob 2013-05-05 00:12:40

+0

我應該只是刪除這些視圖控制器之間的segue連接?以及如何使用「vc」傳遞數據? – AJ112 2013-05-05 00:14:09

+0

@ AJ112是的,你需要刪除賽格。你的方法的其餘部分不會改變,從使用'detailViewController.passedData = object;'開始傳遞數據。 – dasblinkenlight 2013-05-05 00:15:52

相關問題