[英]Passing Data Between View Controllers DidSelectRowsAtIndexPath Storyboards
在一個簡單的測試應用程序中,我試圖將一個名為“thisArray”的數組對象從MasterViewController
傳遞給DetailViewController
名為“passedData”的字符串。 我正在使用Storyboard, UIViewController
嵌入在導航控制器中。 使用prepareForSegue
方法,我成功傳遞了UIViewController
之間的數據:
- (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
之間的segue連接?
3)如果視圖控制器之間確實沒有segue連接,我怎樣才能使用didSelectRowAtIndexPath
在它們之間傳遞數據?
謝謝!
更新:根據我收到的答案和評論,我寫了以下內容:
首先我刪除了控制器之間的segue連接,設置了StoryBoard 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];
}
但它因以下錯誤而崩潰:
***因未捕獲的異常而終止應用程序
NSInvalidArgumentException
,原因:'無法在bundle中找到名為DetailViewController
的故事板
你的第二次嘗試幾乎是對的。 唯一不太正確的是從故事板中實例化視圖控制器的方式:使用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"];
有關詳情,請參閱此問題 。
完成替換后,您的代碼應按預期工作。
編輯::這是你的方法的樣子:
- (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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.