在一個簡單的測試應用程序中,我試圖將名爲「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 namedDetailViewController
in bundle
見dasblinkenlight的編輯,並更改更新的代碼給他。它應該照顧你正在嘗試做的事情!:) – lnafziger 2013-05-05 02:21:19
是的,它完成了。謝謝! – AJ112 2013-05-05 04:06:53