Lee Sugden
Lee Sugden

Reputation: 63

UICollectionViewCell Custom Segue ViewController Connection error

at current i have the following code as shown below:

GroupsViewController.m

#import "GroupsViewController.h"
#import "GroupsHomeViewController.h"
#import "CustomCell.h"

@interface GroupsViewController ()
{
    NSArray *arrayOfImages;
    NSArray *arrayOfDescriptions;
    NSString * _titleForNextVC;
}

@end

@implementation GroupsViewController
{
    NSString *reuseIdentifier;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[self GroupsCollectionView]setDataSource:self];
    [[self GroupsCollectionView]setDelegate:self];
    reuseIdentifier= @"SmallIcon";

    arrayOfImages = [[NSArray alloc]initWithObjects:@"A.png",@"B.png",@"C.png",nil];

    arrayOfDescriptions = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",nil];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [arrayOfDescriptions count];
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    [[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
    [[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = (CustomCell *) [self collectionView:collectionView cellForItemAtIndexPath:indexPath];
    _titleForNextVC = cell.IconLabel.text;
}

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

    if ([segue.identifier isEqualToString:@"GroupsHomeSegue"]) {

        GroupsHomeViewController *vc = (GroupsHomeViewController *)segue.destinationViewController;
        vc.titleText = _titleForNextVC;
    }
}

- (void)setTitleText:(NSString *)titleText {

    _titleText = _titleForNextVC;

    // Set Title of your ViewController
    self.title = _titleText;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    //Dispose of any resources that can be recreated.
}

// Toggle View Button
- (IBAction)cellToggleAction:(id)sender {

    if([reuseIdentifier isEqualToString:@"SmallIcon"]){
        reuseIdentifier=@"ListView";
        [sender setImage:[UIImage imageNamed:@"LargeIcon"]];
    }
    else if
        ([reuseIdentifier isEqualToString:@"ListView"]){
        reuseIdentifier=@"LargeIcon";
        [sender setImage:[UIImage imageNamed:@"SmallIcon"]];
    }
    else if
        ([reuseIdentifier isEqualToString:@"LargeIcon"]){
        reuseIdentifier=@"SmallIcon";
        [sender setImage:[UIImage imageNamed:@"ListView"]];
    }

    [self.GroupsCollectionView reloadData];
}

//Toggled Cell Sizes
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CGSize cellSize;

    if([reuseIdentifier isEqualToString:@"SmallIcon"])
        cellSize = CGSizeMake(100, 130);
    else if
        ([reuseIdentifier isEqualToString:@"ListView"])
        cellSize = CGSizeMake(320, 50);
    else if
        ([reuseIdentifier isEqualToString:@"LargeIcon"])
        cellSize = CGSizeMake(320, 360);

    return cellSize;
}

@end

GroupsHomeViewController.m

#import "GroupsHomeViewController.h"

@interface GroupsHomeViewController ()

@end

@implementation GroupsHomeViewController

-(void)viewDidLoad{
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end   

Well the problem seems to be that i cannot get to my new ViewController via my custom segue once i have clicked on one of my UiCollectionViewcells. Basically i click a cell in my UICollectionView and the title disappears but it does nothing else. It should open up GroupsHomeViewController and set the title of the View Controller to be the label that is placed within the cell i have just clicked. I can't even see if my current title will even work either as i can't get myGroupsHomeViewController to display.

I assume i am missing a line of code somewhere along the lines, but i am struggling to find out where or what it can be that due to receiving no error message's at all.

Also i would just like to point out that i am new to this and have only been developing my app in my spare time for the past month or so. So it would be greatly appreciated if you were to help me with this problem and i thank you in advance for any direction as to what i may be missing.

Upvotes: 0

Views: 56

Answers (1)

David Yang Liu
David Yang Liu

Reputation: 1170

So the problem is you need to save off "what ever" your trying to pass to the next view controller in a property. In your case i think you have in _titleForNextVC

next is you need to name your segue in your storyboard e.g "GroupsHomeSegue" then you can

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = (CustomCell *) [self collectionView:collectionView cellForItemAtIndexPath:indexPath];
    _titleForNextVC = cell.IconLabel.text;

  [self performSegueWithIdentifier:@"GroupsHomeSegue" sender:self];
}

then it would work

Upvotes: 1

Related Questions