I'm currently building a tableview with drill down and I'm running into some issues. I'm using prepareforsegue to call the next view and I'm using didselectrowatIndexpath to pass data from the source view to the destination view.
I have the following problem: 1) When I select a cell, the prepareforsegue is called before the didSelectRowAtIndexPath which means the view is called before my data can be passed into the next view 2) I have a performseguewithidentifier in my didselectrowatIndexpath and it seems to be calling the segue twice (once initial with the prepareforsegue and the second time with the performseguewithidentifier).
What is the best practice in using storyboard to pass data from one tableview to another and what is the best way to call the next view?
Here are some of my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (level == 0) {
int muscleID = [muscleIDArray[indexPath.row] intValue];
NSLog(@"MuscleID: %i", muscleID);
NSArray *submuscleArray = [self submuscleGroup:muscleID valueForKeyPath:@"submuscleID"];
if (submuscleArray == nil || [submuscleArray count] == 0) {
currentMID = muscleID;
level = 2;
[self performSegueWithIdentifier:@"drillDown2" sender:self];
} else {
currentMID = muscleID;
level = 1;
[self performSegueWithIdentifier:@"drillDown" sender:self];
}
} else if (level == 1) {
level = 2;
currentSMID = [submuscleIDArray[indexPath.row] intValue];
[self performSegueWithIdentifier:@"drillDown2" sender:self];
} else {
detailTitle = [[exerciseList valueForKey:[exerciseListSorted objectAtIndex:indexPath.row]] objectAtIndex:0];
exerciseID = [[exerciseListSorted objectAtIndex:indexPath.row] intValue];
[self performSegueWithIdentifier:@"showExerciseDetail" sender:self];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"showExerciseDetail"]) {
//ExerciseDetail *exerciseDetail = (ExerciseDetail *)segue.destinationViewController;
//exerciseDetail.pageTitle = detailTitle;
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
ExerciseDetail *exerciseDetail = (ExerciseDetail *)navController.topViewController;
exerciseDetail.title = detailTitle;
exerciseDetail.exerciseID = exerciseID;
} else if ([segue.identifier isEqualToString:@"drillDown"] || [segue.identifier isEqualToString:@"drillDown2"]){
//UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
ExerciseTableViewController *tableView = segue.destinationViewController;
tableView.delegate = self;
tableView.level = level;
tableView.currentSMID = currentSMID;
tableView.currentMID = currentMID;
NSLog(@"level: %i, currentSMID: %i, currentMID: %i", level, currentSMID, currentMID);
} else if ([segue.identifier isEqualToString:@"addExercise"]){
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
AddExerciseViewController *addExercise = (AddExerciseViewController *)navController.topViewController;
addExercise.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
} else if ([segue.identifier isEqualToString:@"dismissView"]){
[self dismissViewControllerAnimated:YES completion:nil];
}
}