I have 3 different UIButtons for each image grouped. I have IDs for each image. Right now, I have a special id for each image, and I set the button with that tag.
I want to change the background image of the one selected when you tap it. The problem is, is that 3 buttons have the same tag so I cannot change the right button’s background image.
Here’s what I have:
UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[likeButton setBackgroundColor:[UIColor clearColor]];
[likeButton setBackgroundImage:[UIImage imageNamed:@"icon_like_button.png"] forState:UIControlStateNormal];
[likeButton setBackgroundImage:[UIImage imageNamed:@"icon_like_button_hit.png"] forState:UIControlStateSelected];
[likeButton setBackgroundImage:[UIImage imageNamed:@"icon_like_button_hit.png"] forState:UIControlStateHighlighted];
[likeButton setBackgroundImage:[UIImage imageNamed:@"icon_like_button_hit.png"] forState:UIControlStateDisabled];
[likeButton setFrame:CGRectMake(13, 52 + (285 * count), 51, 55)];
[likeButton addTarget:self action:@selector(likeDudle:) forControlEvents:UIControlEventTouchDown];
[likeButton setTag:theIdInt];
[likeButton setTitle:@"no_like" forState:UIControlStateNormal];
[scrollView addSubview:likeButton];
- (IBAction)likeDudle: (id)sender {
NSInteger tagId = ((UIControl*)sender).tag;
UIButton *tempButton = (UIButton*)[scrollView viewWithTag:tagId];
NSLog(@"likeDudle: %d // %@", tagId, tempButton.titleLabel.text);
if ([tempButton.titleLabel.text isEqualToString:@"no_like"]) {
[tempButton setBackgroundImage:[UIImage imageNamed:@"icon_like_button_hit.png"] forState:UIControlStateNormal];
[tempButton setTitle:@"like" forState:UIControlStateNormal];
} else if ([tempButton.titleLabel.text isEqualToString:@"like"]) {
[tempButton setBackgroundImage:[UIImage imageNamed:@"icon_like_button.png"] forState:UIControlStateNormal];
[tempButton setTitle:@"no_like" forState:UIControlStateNormal];
}
Is there a better way in doing this?
Thanks,
Coulton
If you have fewer than 10 images, then give the
ith button the tagi*10+image.tag. Then you can retrieve theimage.tagbybutton.tag % 10and the button tags will be unique. You can even retrieve the button only information byint b = button.tag/10.Also, you can access the button’s image’s tag with
button.backgroundImage.tag, so then the button could have its own separate tagging system, depending on your uses.