I’m presenting a VC modally, then I dismiss it when a cell is selected along with call a method from the original VC.
The problem now is that nav and routineTableViewController NSLog as null.
How I present the model VC:
NSString *selectedRow = [[self.exerciseArray objectAtIndex:selectedRowIndex.row]objectForKey:@"exerciseName"];
UINavigationController *nav = self.parentViewController.navigationController;
RoutineDayTableViewController *routineDayTableViewController = (RoutineDayTableViewController *) [nav topViewController];
routineDayTableViewController.muscleURL = self.muscleURL;
[routineDayTableViewController addExercise];
[self.parentViewController dismissModalViewControllerAnimated:YES];
Hierarchy of VC: RoutineDayTableViewController -> presents Modally MusclesTableViewController, then pushes to child table view, which dismisses back to RoutineDayTableViewController.
Edit:
-(void)addExercise
{
PFObject *exerciseInRoutine = [[PFObject alloc] initWithClassName:@"exerciseInRoutine"];
[exerciseInRoutine setObject:self.selectedExercise forKey:@"name"];
[exerciseInRoutine setObject:self.muscleName forKey:@"muscle"];
[exerciseInRoutine setObject:self.muscleURL forKey:@"picture"];
[exerciseInRoutine saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[self.tableView reloadData];
} else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
See my reply here: How to save nsdictionary of a subview to a mainview based off tableviewcell selection
The ideal solution is to create a protocol and use a delegate to pass information backwards in your view controller hierarchy. This avoids the tight coupling which is happening in your example when you link back to
self.parentViewController.navigationController. What happens if the parent view controller changes in the future or you want to resuse the selection controller elsewhere in the app? The new view controller that is the parent could be an instance of a class other thanRoutineDayTableViewControllerand may not have aexerciseURLproperty.For this specific example, I would do something like the following:
Add the protocol definition inside SpecificExerciseTableViewController.h
Add a property inside MuscleTableViewController.h
Add a property inside SpecificExerciseTableViewController.h
Inside RoutineDayTableViewController.h
Inside RoutineDayTableViewController.m when allocating a
MusclesTableViewControllerinstance:Inside MuscleTableViewController.m when allocating a
SpecificExerciseTableViewControllerinstance:Inside SpecificExerciseTableViewController.m
Finally, in your RoutineDayTableViewController.m
For an example of this in practice in a video see:
00:30:00 min mark in WWDC 2011 Video – Introducing Interface Builder Storyboarding