Search code examples
iosobjective-cuitableviewnsarray

Objective C - Passing data between two UITableViewControllers


Display details in second UITableViewController

My application is a simple Recipebook to understand UITableViewControllers. The application contains two UITableViewControllers. The first UITableViewController contains a UITableView with a list of the recipe names. If you select a cell you will segue to the second UITableViewController. The second UITableViewController contains a UITableView with a list of the ingredients.

The application contains the following classes:

  • RecipeTableViewController (first)
  • IngredientTableViewController (second)
  • RecipeObject
  • RecipeData

The RecipeObject Class contains two properties. One property of type NSString with the recipe name. The other property is of type NSArray with the ingredients. The RecipeObject objects are in the RecipeData class.

RecipeObject *recipe1 = [[RecipeObject alloc]init];
recipe1.name = @"Fresh Coconut Cake";
recipe1.ingredients = [NSArray arrayWithObjects:@"Coconut cups", @"Milk", @"Baking powder", @"Butter", @"Sugar", @"Eggs", nil];

The RecipeData is called in the RecipeTableViewController to display the recipe names in the tableView.

Message from RecipeData class to RecipeTableViewController:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.recipes = [RecipeData allRecipes];}

Display names in the tableView:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellIdentifier = @"recipeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    RecipeObject *recipes = [self.recipes objectAtIndex:indexPath.row];
    cell.textLabel.text = recipes.name;

    return cell;
}

How to add the recipe1.ingredients array to the IngredientsTableViewController?

Any help is highly appreciated!


Solution

  • Perform a segue when a recipe is selected. In the prepare for segue-method take the selected recipe and pass the ingredients to the ingredientsTableViewController. Something quite similar to this:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        [self performSegueWithIdentifier:@"to_Ingredients" sender:self];
    }
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([segue.identifier isEqualToString:@"to_Ingredients"]) {
            IngredientTableViewController *ingredientsTableViewController = (IngredientTableViewController *)[segue destinationController];
    
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        RecipeObject *recipe = [self.recipes objectAtIndex:indexPath.row];
        ingredientsTableViewController.ingredients = recipe.ingredients;
    
        }
    
    }
    

    If you don't use a storyboard where you set this segue you just need the first method which the should looks like this:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    IngredientTableViewController *ingredientsTableViewController = [[IngredientTableViewController alloc]init];
    RecipeObject *recipe = [self.recipes objectAtIndex:indexPath.row];
    ingredientsTableViewController.ingredients = recipe.ingredients;
    [self showDetailViewController:ingredientsTableViewController sender:self]
    }