0

I was wondering if one of you could help me with a little problem I am having, if it is possible. I have a few global variables called ChecklistA, ChecklistB, etc. What I want, is if the user selects a table cell called A, that ChecklistA is passed on to the destination controller.

In other words:
Global variables: NSArrays -> "ChecklistA", "ChecklistB", "ChecklistC", etc.
Cells: "A", "B", "C", etc

... on click of cell B...
destinationController.checklist = NSArray named: "ChecklistB"

I hope this is clear, if you have any questions feel free to ask.

Thank you in advance!

lwm
  • 15
  • 5

1 Answers1

0

I believe your table view has this delegate method implemented:

tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

You get notified whenever a cell is selected(tapped) by the user.

You can implement your delegate method in this way:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   if( indexPath.row == 0) /// First cell
   {
       _selectedChecklist = ChecklistA;
   }
   else if( indexPath.row == 1) /// Second cell
   {
       _selectedChecklist = ChecklistB;
   }
   else if( indexPath.row == 2) /// Third cell
   {
       _selectedChecklist = ChecklistC;
   }
}

for your prepareForSegue, simply pass the value to your destination viewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   destinationController.checklist = _selectedChecklist; 
}
Casey
  • 103
  • 6
  • I was hoping to implement this method without using else if, etc. Is there anyway to do it the way I described in my question? – lwm Aug 18 '14 at 03:27
  • If you declared the variables as property within the class, you can use `[self valueForKey:@""]`, else you can try adding them in an array or dictionary then retrieve them with the index/key accordingly. Similar question has been asked here : [link](http://stackoverflow.com/questions/2283374/objective-c-equivalent-of-phps-variable-variables) – Casey Aug 18 '14 at 03:40