如何在单击三层上设置 UIBarButtonItem 图像 [英] How to set UIBarButtonItem Image on click three tier
问题描述
我当前的代码如下,如果可能的话,我想知道如何在当前的 if 语句中实现按钮图标更改,如果这不可能,那么我需要使用什么代码来代替?非常感谢.
Hi there my current code is as follows and i would like to know how to implement a button icon change within my current if statement if possible if this is not possible then what code will i need to use instead? Many thanks in advance.
viewController.m
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
在实施您当前的建议后,我收到以下错误消息:
After implementing your current suggestions i receive the following error message:
[UIBarButtonItem setImage:forState:]: unrecognized selector sent to instance
推荐答案
事实证明,sender 是 UIBarButtonItem
对于这个类,使用方法 setBackgroundImage:forState:barMetrics:
代替 setImage:forState:
,如下面的原始帖子中的 UIButton.还将 sender 类型转换为 UIBarButtonItem
For this class use the method setBackgroundImage:forState:barMetrics:
in replace of setImage:forState:
as in the original post below which is for a UIButton. Also typecast sender to UIBarButtonItem
[(UIBarButtonItem*)sender setBackgroundImage:[UIImage imageNamed:@"scion.png"] forState:UIControlStateNormal barMetrics: UIBarMetricsDefault];
原始帖子:假设 (id)sender 是您要在名为 -cellToggleAction:(id)sender的方法中更改的 UIButton 实例code>,将 sender 类型转换为 UIButton,然后在 if 语句中调用
-setImage:forState:
.
ORIGINAL POST:
Assuming (id)sender is the UIButton instance you want to change in the method named -cellToggleAction:(id)sender
, typecast sender to a UIButton and then call -setImage:forState:
on it in the if statement.
或者,您可以将上述方法更改为具有 UIButton 而不是 id 类型的参数,以避免类型转换,也使代码更具可读性.
Alternatively you could change the above mentioned method to have an argument of type UIButton rather than id, so as to avoid the typecast and it also makes the code more readable.
一个例子可能是 -
- (IBAction)cellToggleAction:(UIButton*)sender {
if([reuseIdentifier isEqualToString:@"SmallIcon"]) {
reuseIdentifier=@"ListView";
[sender setImage:yourImage forState:UIControlStateNormal];// may need other states configured
}
else if .. // continue similarly
这篇关于如何在单击三层上设置 UIBarButtonItem 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!