簡體   English   中英

如何將值從嵌入式collectionView中的選定單元格傳遞到另一個viewController?

[英]How to pass value from a selected cell in embedded collectionView to another viewController?

UIViewController 1 ,我設置了一個數組。 UIViewControllerUIViewController 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];
  }
}

我一直在返回空值,但我不確定為什么。

(我不使用情節提要。這是我第一次嘗試任何類型的編程。不勝感激!)

您可以使用委派或完成塊來完成。

要與代表團一起解決,請點擊此鏈接

要解決完工問題,請點擊此鏈接

您可以使用UICollectionViewDelegate

添加您的課程:

class YourViewController: UIViewController, UICollectionViewDelegate { ... }

並使用- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath選擇單元格時將調用此事件; 您必須將值保存在屬性中; 像那樣:

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

接着:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if ([segue.identifier isEqualToString:@"showRecipePhoto"]) {
    RecipeViewController *destViewController = segue.destinationViewController;
    destViewController.recipeImageName = selectedRecipeImageName
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM