Dancer
Dancer

Reputation: 17671

IOS - UICollectionView - Pass data to child view via segue

I'm struggling with passing data to a child view via a UICollectionView - basically I want to populate several UITextField in the child view with the relevant data based on item clicked - this is a screenshot of my view -

enter image description here

Here is a snipped of the array data to show how its put together -

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

the array is currently static - so its compiled as follows -

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

The uiCollectionView is populated as follows -

- (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; 

}

I have stored the selected index path in this method -

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


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

}

finally i try and pass the data as follows -

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


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

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




}

but I get an error stating property siteTitle not found on NSArray..

Where am i going wrong?

Upvotes: 0

Views: 2024

Answers (2)

vacawama
vacawama

Reputation: 154603

Your _sightArray contains objects of type sightsObject. Try:

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

Upvotes: 1

user2071152
user2071152

Reputation: 1195

You can achieve the same using

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

In desController.h file declare the string variable as

@property(nonatomic, copy) NSString *selString;

Upvotes: 1

Related Questions