目前,我有以下代码,如下所示:
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
之一,我就无法通过自定义键进入新的UiCollectionViewcells
。基本上,我单击UICollectionView
中的一个单元格,标题消失了,但是它什么也没做。它应该打开GroupsHomeViewController
并将View Controller的标题设置为我刚刚单击的单元格中放置的标签。我什至看不到我当前的标题是否也能正常工作,因为我无法显示我的GroupsHomeViewController
。我以为我遗漏了某行代码,但是由于根本没有收到任何错误消息,我正在努力找出可能在哪里或什么地方。
另外,我想指出的是,我对此并不陌生,过去一个月左右的时间里我一直只是在业余时间开发我的应用程序。因此,如果您能帮助我解决这个问题,将不胜感激,对于您可能缺少的任何指导,我先谢谢您。
最佳答案
因此,问题在于您需要保存尝试传递给属性中下一个视图控制器的“无论如何”。就您而言,我认为您在_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];
}
然后它会工作