[英]How to pass value from a selected cell in embedded collectionView to another viewController?
In UIViewController 1
, I have set up an array. 在UIViewController 1
,我设置了一个数组。 This UIViewController
segues to UIViewController 2
. 此UIViewController
与UIViewController 2
。
In UIViewController 2
, there is a UITableView
with custom UITableViewCell
. 在UIViewController 2
,有UITableView
自定义UITableViewCell
。 There's also a UIButton
which segues perfectly fine back to UIViewController 1
. 还有一个UIButton
可以完美地返回UIViewController 1
。
In the custom cell, there is a collectionView
. 在自定义单元格中,有一个collectionView
。 This is populated by the array from ViewController 1
. 这是由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
? 我的问题是,当在collectionView
( UIViewController 2
自定义UITableViewCell
类)中选择了一个项目时,如何将该值一直传递回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/ 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!) (我不使用情节提要。这是我第一次尝试任何类型的编程。不胜感激!)
You can use UICollectionViewDelegate
. 您可以使用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; 并使用- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
选择单元格时将调用此事件; 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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.