How to set UIBarButtonItem Image on click three tier

南楼画角 提交于 2019-12-13 07:23:52

问题


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

   #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

回答1:


EDIT: As it turns out sender is an instance of 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];

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.

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.

an example could be -

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

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


来源:https://stackoverflow.com/questions/35073751/how-to-set-uibarbuttonitem-image-on-click-three-tier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!