gpt4 book ai didi

ios - UICollectionViewCell 自定义 Segue ViewController 连接错误

转载 作者:行者123 更新时间:2023-11-29 01:15:05 25 4
gpt4 key购买 nike

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

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

好吧,问题似乎是,一旦我点击了我的一个UiCollectionViewcells,我就无法通过自定义segue访问我的新ViewController。基本上,我单击 UICollectionView 中的一个单元格,标题就会消失,但它没有执行其他任何操作。它应该打开 GroupsHomeViewController 并将 View Controller 的标题设置为我刚刚单击的单元格中放置的标签。我什至看不到我当前的标题是否有效,因为我无法显示我的GroupsHomeViewController

我假设我在某处遗漏了一行代码,但由于根本没有收到错误消息,我正在努力找出它在哪里或可能是什么。

另外,我想指出,我对此很陌生,并且在过去一个月左右的时间里才在业余时间开发我的应用程序。因此,如果您能帮助我解决这个问题,我将不胜感激,并且我提前感谢您提供有关我可能缺少的内容的任何指示。

最佳答案

所以问题是你需要保存你试图传递给属性中的下一个 View Controller 的“任何东西”。在你的情况下,我认为你有 _titleForNextVC

接下来你需要在 Storyboard中命名你的 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];
}

然后就可以了

关于ios - UICollectionViewCell 自定义 Segue ViewController 连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35278892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com