IOS - UICollectionView - 通过 segue 将数据传递给 subview

标签 ios objective-c

我正在努力通过 UICollectionView 将数据传递给 subview - 基本上我想在 subview 中使用基于项目的相关数据填充几个 UITextField单击 - 这是我 View 的屏幕截图 -

enter image description here

这里是数组数据的片段,展示了它是如何组合在一起的——

 sightsObject *sight13 = [[sightsObject alloc] initWithsiteTitle:@"ssd"   siteSection:@"views" siteType:@"" siteSubTitle:@"" siteDescription:@"" siteMapUrl:@"docks" sitePic:@"further"];

数组目前是静态的——所以它的编译如下——

 self.sightsArray = [NSArray arrayWithObjects:sight1, sight2, sight3, sight4, sight5, sight6, sight7, sight8,sight9,sight10, sight11, sight12,sight13, nil];

 self.sectionData = [NSArray arrayWithObjects:@"beatles", @"docks",@"unique",@"museums", @"Football",@"Tours",@"Views",@"Further Afield", nil];

uiCollectionView 填充如下 -

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

_selectedItemIndex = indexPath.row;


CSCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"
                                                        forIndexPath:indexPath]
NSString *s = self.sectionData[indexPath.section];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"siteSection like %@", s];
NSArray *filteredArr = [self.sightsArray filteredArrayUsingPredicate:pred];
NSLog(@"Value of hello = %@", filteredArr);
sightsObject *rightS = filteredArr[indexPath.row];
cell.textLabel.text =rightS.siteTitle;
cell.textBrief.text = rightS.siteSubTitle;
cell.imageIco.image = [UIImage imageNamed:rightS.sitePic];

return cell; 

我已经在这个方法中存储了选择的索引路径——

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {


   self.selectedItemIndex = indexPath.row; //stored as NSInteger

}

最后我尝试按如下方式传递数据 -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{


    NSArray* selectedItem = [_sightsArray objectAtIndex:self.selectedItemIndex ];

    SightViewController *destController = [segue destinationViewController];
    destController.viewTit = selectedItem.siteTitle ;




}

但我收到一条错误消息,指出在 NSArray 上找不到属性 siteTitle..

我哪里错了?

最佳答案

你可以用

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"FirstView"]) {
        NSString* selectedItem = [_sightsArray objectAtIndex:self.selectedItemIndex];
        SightViewController *destController = (SightViewController *)segue.destinationViewController;
        destController.selString = selectedItem;
    }
}

在 desController.h 文件中将字符串变量声明为

@property(nonatomic, copy) NSString *selString;

关于IOS - UICollectionView - 通过 segue 将数据传递给 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23632332/

相关文章:

ios - [NSNull 长度] : unrecognized selector sent to instance on keyboard keystroke?

ios - UIControl 跟踪在不同 UIControl 上开始的触摸

ios - 运行 Non-Storyboard 应用程序的运行时错误

iphone - 为什么这段代码在 iOS 5.1 中会崩溃,而在 iOS 6 中不会?

objective-c - 从 Cocoa 发送电子邮件

ios - 在 Sprite Kit 中创建按钮的正确方法?

ios - 使用 Swift 更新实体记录而不是使用 CoreData 创建新记录

ios - 无法在 Swift 3 中使用闭包语法

ios - Xcode - 错误 ITMS-90635 - bundle 中的 Mach-O 无效 - 正在提交到应用商店

objective-c - Markdown 到 uiview/nsview