2012-08-09 59 views
1

我想將視圖控制器中的變量傳遞給另一個視圖控制器,以便當用戶在第一個表視圖中選擇某一行時,應用程序將把他帶到另一個視圖控制器,其中將顯示所選項目的詳細信息。 這是我的代碼:如何使用didSelectRowAtIndexPath將變量從uitableview傳遞給第二個tableview?

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


    NSString *selectedAuthors = [theauthors objectAtIndex:indexPath.row]; 


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
    Details *dvController = [storyboard instantiateViewControllerWithIdentifier:@"Details"]; //Or whatever identifier you have defined in your storyboard 





    dvController.selectedAuthors = selectedAuthors; 

    UIAlertView *messageAlert = [[UIAlertView alloc] 
           initWithTitle:@"Row Selected" message:authorNAme delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

    // Display Alert Message 
    authorNAme = [[NSString stringWithFormat:[theauthors objectAtIndex:indexPath.row]] intValue]; 
[messageAlert show]; 

    [self.navigationController pushViewController:dvController animated:YES]; 
} 

selectedAuthors是一個字符串 AUTHORNAME是,我希望存儲選定行的內容的全局變量。 任何幫助將不勝感激。

+0

by'dv.selectedAuthors = selectedAuthors;''你已經將數據傳遞給第二個視圖控制器。您應該在詳細信息視圖控制器中顯示基於selectedAuthors的詳細信息。 – Moxy 2012-08-10 01:56:24

+0

請檢查鏈接它會顯示您的具體問題:http://stackoverflow.com/questions/11897149/how-to-send-the-details-of-a-selected-cell-to-another-view-controller – 2012-08-10 07:26:39

回答

-1
NSString *titleString = [[[NSString alloc] initWithFormat:@"Element number : %d",indexPath.row] autorelease]; 

這是用來發送變量的最簡單的函數,它只是從無處出現!對於任何人丟失這是最好的功能!

1

我看到你正在使用故事板。

在這種情況下,將信息發送到您的下一個視圖控制器實現

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 

調用此方法每次SEGUE即將被執行的時間。 例如,在didSelectRowAtIndexPath中,您可以將行的信息保存在一個字典中,並將其傳遞給prepareForSegue方法中的下一個視圖控制器。

有關詳細信息,您可以查看Apple的名爲SimpleDrillDown的示例項目。它做同樣的事情你需要做的: http://developer.apple.com/library/ios/#samplecode/SimpleDrillDown/Introduction/Intro.html

+0

作爲Xcode的新手,我已經嘗試過這個建議,但它沒有工作..你可以給我一個片段PLZ。非常感謝 – 2012-08-09 11:47:33

+0

請檢查此鏈接:http://stackoverflow.com/questions/11897149/how-to-send-the-details-of-a-selected-cell-to-another-view-controller – 2012-08-10 07:27:01

+0

您是否嘗試過看看我建議你的項目?如果您有問題並且無法正常工作,請更新您的問題,以便我可以看到問題所在。 – 2012-08-16 10:42:41

相關問題