gpt4 book ai didi

ios - 如何在单击三层时设置 UIBarButtonItem 图像

转载 作者:行者123 更新时间:2023-11-29 01:16:51 24 4
gpt4 key购买 nike

您好,我当前的代码如下,我想知道如何在当前的 if 语句中实现按钮图标更改(如果可能),如果不可能,那么我需要使用什么代码?非常感谢。

viewController.m

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

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

@end

@implementation GroupsViewController
{
NSString *reuseIdentifier;
}

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

arrayOfImages = [[NSArray alloc]initWithObjects:@"?.png", nil];

arrayOfDescriptions = [[NSArray alloc]initWithObjects:@"?", 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)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}

- (IBAction)cellToggleAction:(id)sender {

if([reuseIdentifier isEqualToString:@"SmallIcon"]){
reuseIdentifier=@"ListView";
[sender setImage:[UIImage imageNamed:@"scion.png"] forState:UIControlStateNormal];
}
else if
([reuseIdentifier isEqualToString:@"ListView"]){
reuseIdentifier=@"LargeIcon";
[sender setImage:[UIImage imageNamed:@"subaru.png"] forState:UIControlStateNormal];
}
else if
([reuseIdentifier isEqualToString:@"LargeIcon"]){
reuseIdentifier=@"SmallIcon";
[sender setImage:[UIImage imageNamed:@"lotus.png"] forState:UIControlStateNormal];
}

[self.GroupsCollectionView reloadData];
}

- (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, 130);
else if
([reuseIdentifier isEqualToString:@"LargeIcon"])
cellSize = CGSizeMake(320, 350);

return cellSize;
}
@end

在实现您当前的建议后,我收到以下错误消息:

[UIBarButtonItem setImage:forState:]: unrecognized selector sent to instance

最佳答案

编辑:结果是 senderUIBarButtonItem 的一个实例

对于此类,使用方法 setBackgroundImage:forState:barMetrics:代替setImage:forState:就像下面的原始帖子一样,它是针对 UIButton 的。同时将 sender 类型转换为 UIBarButtonItem

[(UIBarButtonItem*)sender setBackgroundImage:[UIImage imageNamed:@"scion.png"] forState:UIControlStateNormal barMetrics: UIBarMetricsDefault];

原帖:假设 (id)sender 是您要在名为-cellToggleAction:(id)sender 的方法中更改的UIButton 实例, 将 sender 类型转换为 UIButton 然后调用 -setImage:forState:在 if 语句中。

或者,您可以将上述方法更改为具有类型为 UIButton 而不是 id 的参数,以避免类型转换,这也使代码更具可读性.

一个例子可能是-

- (IBAction)cellToggleAction:(UIButton*)sender {

if([reuseIdentifier isEqualToString:@"SmallIcon"]) {
reuseIdentifier=@"ListView";
[sender setImage:yourImage forState:UIControlStateNormal];// may need other states configured
}
else if .. // continue similarly

关于ios - 如何在单击三层时设置 UIBarButtonItem 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35073751/

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