0

In UIViewController 1, I have set up an array. This UIViewController segues to UIViewController 2.

In UIViewController 2, there is a UITableView with custom UITableViewCell. There's also a UIButton which segues perfectly fine back to UIViewController 1.

In the custom cell, there is a collectionView. This is populated by the array from ViewController 1.

My question is, when an item is selected in the collectionView (UIViewController 2 - custom UITableViewCell class), how to pass that value all the way back to UIViewController 1?

I'm sorry if this is repetitive. I've referred to many similar entries here but nothing seems to be working. I've also tried this:

http://www.appcoda.com/ios-collection-view-tutorial/

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:@"showRecipePhoto"]) {
    NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
    RecipeViewController *destViewController = segue.destinationViewController;
    NSIndexPath *indexPath = [indexPaths objectAtIndex:0];
    destViewController.recipeImageName = [recipeImages[indexPath.section] objectAtIndex:indexPath.row];
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
  }
}

I keep getting the null value returned and I'm not sure why.

(I'm not using storyboard. And this is my first attempt at programming of any kind. Would appreciate any input!)

veve1127
  • 3
  • 1

2 Answers2

0

You can do it either with delegation or completion block.

To solve with delegation please follow this link.

To solve with completion block please follow this link.

  • Welcome to SO. Please avoid link-only as answers. You can use the "flag" button to flag as a duplicate if you think this has been answered already. –  Aug 30 '17 at 19:48
0

You can use UICollectionViewDelegate.

Add in your class:

class YourViewController: UIViewController, UICollectionViewDelegate { ... }

and use - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath this event is called when the cell is selected; you must save the value in a property; like that:

- (void)collectionView:(UICollectionView *)collectionView 
didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{
   selectedRecipeImageName = [recipeImages[indexPath.section] objectAtIndex:indexPath.row];
... 
   [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}

and then:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:@"showRecipePhoto"]) {
    RecipeViewController *destViewController = segue.destinationViewController;
    destViewController.recipeImageName = selectedRecipeImageName
  }
}
Kevinosaurio
  • 1,952
  • 2
  • 15
  • 18
  • Yes. So simple yet I couldn't wrap my head around it at the time. By the way, sorry for the delayed reply. I'm doing this on the side. Thank you! – veve1127 Sep 04 '17 at 13:24