Next to my other answer I want to show another way that I wasnt aware of till just few minutes ago: Using an unwinding segue instead of delegation.
An unwinding segue will some how reverse the segue that brought us to a view controller. "Some how" means that you actually dont need to return to the previous controller, but a previous one. When creating a unwinding segue you connect it with a target of the signature
-(IBAction)actionName:(UIStoryboardSegue *)segue
in one of the former view controller. For details see here: Using Xcode Storyboarding
The segue the action is called with will contain the source controller, in our case the detail view controller. We can now access an property or method we defined on it.
@interface StateSelctionViewController : UITableViewController
@property (nonatomic, strong) NSString *selectedNation;
@property (nonatomic, strong, readonly) NSString *selectedState;
@end
I made the selectedState readonly so that it is clear that this cant be set but should be read after selection. I re-declare it readwrite in a class extension.
@interface StateSelctionViewController ()
@property (nonatomic, strong, readwrite) NSString *selectedState;
@end
in the storyboard i created a manually unwinding segue as shown in Technical Note TN2298: Figure 2
As soon as a row is selected, I will set selectedState
and perform the unwinding segue
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.selectedState = stateDictionary[_selectedNation][indexPath.row];
[self performSegueWithIdentifier:@"unwind" sender:self];
}
This will execute the hooked up action in the Master View Controller
-(void)returned:(UIStoryboardSegue *)sender
{
NSLog(@"%@", NSStringFromSelector(_cmd));
[self.statesDictionray setObject:[sender.sourceViewController selectedState]
forKey:[sender.sourceViewController selectedNation]];
[self.tableView reloadData];
}
I created a sample code: https://github.com/vikingosegundo/StateSelectionUnwind