PDA

View Full Version : How to pass variable from uitableview to second tableview using didSelectRowAtIndexPa




EliasRahme
Aug 9, 2012, 01:16 AM
i want to pass the variable from a view controller to another one, so that when the user selects a certain row in the first table view, the application will take him to another view controller in which the details of the selected item will appear.
This is my code :


- (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 is a string
authorName is a global variable in which i want to store the content of the selected row.
Any help will be highly appreciated.



Duncan C
Aug 9, 2012, 08:51 AM
i want to pass the variable from a view controller to another one, so that when the user selects a certain row in the first table view, the application will take him to another view controller in which the details of the selected item will appear.
This is my code :


- (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 is a string
authorName is a global variable in which i want to store the content of the selected row.
Any help will be highly appreciated.


Your setup seems reasonable (aside from the fact that global variables are best avoided, since they cause tight coupling between your application classes.)

It looks like you added a property, selectedAuthors, to your Details view controller, which is a good way to pass data. You're creating a new view controller from your Storyboard, setting a property, and pushing it onto the nav controller. That's a little unconventional, since using segues is the normal way to transition between view controllers in a storyboard, but at a glance it looks like it should work.

What is and isn't working about your code?

Do you see the value in selectedAuthors in your Details view controller's viewWillAppear method?

Note that you should not try to assign your selectedAuthors property to a view until viewDidLoad (or better yet, viewWillAppear), since your views may not be loaded yet.