What I’m trying to do is pass the currently selected row in a UITableView to another view controller that’s also a UITabelView. It works (kind of) but my variables are playing leapfrog with each other (seem to be 1 off for some reason.)
Example:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
chosenProject = [[self activeProjectsList] objectAtIndex:[indexPath row]];
self.selectedProject = chosenProject;
[self setSelectedProject:self.selectedProject];
NSLog(@"PROJECT: %@",chosenProject); // Returns correctly.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"ShowShots"]) {
NSLog(@"CURRENT: %@",chosenProject); // Returns NULL?
ShotListViewController *slVC = [segue destinationViewController];
slVC.outputName = self.selectedProject;
}
}
It’s strange because the first time it will return NULL, then when you try again it returns the last selected row that should have been presented the first time.. not NULL.
I’m guessing that the variable is not being set before prepareForSegue is sent which is why it seems to be always one behind when it receives.
Am I going about this wrong, just to pass one variable?
You are correct in your assumption.
tableView:didSelectRowAtIndexPathis called after the row is selected and after the segue is called.You need to put this code in
tableView:willSelectRowAtIndexPathinstead, so that it is called before the segue is done.