如何设置UIBarButtonItem图片点击三层

您好我目前的代码如下,我想知道如何实现在我当前的if语句中的button图标更改,如果可能的话,如果这是不可能的,那么我需要什么代码来代替? 提前谢谢了。

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 

实施您当前的build议后,我收到以下错误信息:

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

编辑:事实certificate, 发件人是UIBarButtonItem的一个实例

对于这个类,使用方法setBackgroundImage:forState:barMetrics:replacesetImage:forState:如下面的原始文章是UIButton还将types转换发送UIBarButtonItem

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

ORIGINAL POST: 假设 (id)发件人是你想在名为-cellToggleAction:(id)sender的方法中改变的UIButton实例,将该-cellToggleAction:(id)sender转换为UIButton ,然后在if语句中调用-setImage:forState:

或者你可以改变上面提到的方法有一个types的UIButton而不是id的参数,以避免types转换,它也使得代码更具可读性。

一个例子可能是 –

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