UICollectionViewCell自定义Segue ViewController连接错误

时间:2016-02-08 20:40:21

标签: ios objective-c uicollectionview segue uicollectionviewcell

目前我有以下代码如下所示:

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   

问题似乎是,一旦我点击了ViewController之一,我无法通过自定义segue访问我的新UiCollectionViewcells。基本上我点击了UICollectionView中的一个单元格,标题消失了,但它什么也没做。它应该打开GroupsHomeViewController并将视图控制器的标题设置为我刚刚单击的单元格中的标签。我甚至无法看到我当前的标题是否会起作用,因为我无法显示GroupsHomeViewController

我假设我在某处某处缺少一行代码,但由于根本没有收到任何错误消息,我正在努力找出它的位置或内容。

另外我想指出的是,我是新手,并且在过去一个月左右的业余时间里只开发了我的应用程序。因此,如果您能帮助我解决这个问题,我将非常感谢,并且我提前感谢您对我可能缺少的任何方向。

1 个答案:

答案 0 :(得分:1)

所以问题是你需要保存试图传递给属性中下一个视图控制器的“什么”。在你的情况下,我认为你有_titleForNextVC

接下来你需要在你的故事板中命名你的segue,例如“GroupsHomeSegue”然后你可以

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

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

然后它会起作用

相关问题