A really simple question here. I have a label on one view and a UITableView on the previous view. I have got a segue triggered when the user selects the row and I want the label to be updated with the text from that row. Here’s one example, the code is pretty obvious.
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *countrySelection;
switch (indexPath.section) {
case kFirstSection:
countrySelection = [[NSString alloc]
initWithFormat:@"The country you have chosen is %@",
[self.MyCountries objectAtIndex: indexPath.row]];
[self performSegueWithIdentifier:@"doneResults" sender:self];
self.countryResult.text = countrySelection;
break;
The label isn’t updated and I just don’t know what should be done.
Thanks in advance!
These kind of things really need to be set on the View Controller that owns them. Use a public property to pass the value of the selected country to that view controller as outlined below:
First, create a property called something like:
in the destination View Controller, and make sure to
@synthesizeitNo reason to create another property for the IndexPath. Just use
in the didSelectRowAtIndexPath method.
Then in the
prepareForSegueMethod:On the
viewDidLoadevent of the Destination VC, just use:* EDIT *
To deal with a datasource that has multiple sections, just use the same logic that you have in the
cellForRowAtIndexPath.N
SDictionary *selRow = [[self.countriesIndexArray valueForKey:[[[self.countriesIndexArray allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:sindexPath.row];Change this to suit your needs, but basically you are implementing the same logic that you would to display a cell, except you are specifying the indexPath (both section and row) that you want.
Then something like the following to set that property on the destination VC: