如何将值从嵌入的collectionView中的选定单元格传递到另一个viewController?

时间:2017-08-30 17:50:05

标签: ios objective-c uitableview uicollectionview

UIViewController 1中,我设置了一个数组。此UIViewController代表UIViewController 2

UIViewController 2中,有UITableView个自定义UITableViewCell。还有一个UIButton可以很好地回到UIViewController 1

在自定义单元格中,有一个collectionView。这是由ViewController 1

中的数组填充的

我的问题是,当collectionViewUIViewController 2 - 自定义UITableViewCell类)中的某个项目被选中时,如何将该值一直传递回UIViewController 1

如果这是重复的话,我很抱歉。我在这里提到了许多类似的条目,但似乎没有任何工作。我也试过这个:

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];
  }
}

我一直得到返回的空值,我不知道为什么。

(我不是在使用故事板。这是我第一次尝试任何类型的编程。非常感谢任何输入!)

2 个答案:

答案 0 :(得分:0)

您可以使用委托或完成块来执行此操作。

要通过授权解决,请遵循此link

要使用完成功能块解决,请按照此link

进行操作

答案 1 :(得分:0)

You can use clipboardData.

Add in your class:

copy

and use document.addEventListener('copy', (event) => { // event.clipboardData.? // maybe event.clipboardData.setData(?) }) this event is called when the cell is selected; you must save the value in a property; like that:

UICollectionViewDelegate

and then:

class YourViewController: UIViewController, UICollectionViewDelegate { ... }