[英]How to pass value from a selected cell in embedded collectionView to another viewController?
在UIViewController 1
,我設置了一個數組。 此UIViewController
與UIViewController 2
。
在UIViewController 2
,有UITableView
自定義UITableViewCell
。 還有一個UIButton
可以完美地返回UIViewController 1
。
在自定義單元格中,有一個collectionView
。 這是由ViewController 1
的數組填充的。
我的問題是,當在collectionView
( UIViewController 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.