2011-07-03 44 views
0

我有3个不同的UIButtons为每个图像分组。我有每个图像的ID。现在,我为每个图像都有一个特殊的ID,并且使用该标签设置按钮。UIButton的特殊标签?

我想更改当您点击它时选择的背景图像。问题是,3个按钮是否具有相同的标签,因此我无法更改右按钮的背景图像。

这是我有:

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]; 
} 

有没有在做这更好的办法?

感谢, 库尔顿

+0

为什么不给每个按钮一个独特的标签? – PengOne

+0

这一切都是从服务器完成的,每个图像都有一个唯一的ID。每个按钮都有一个按钮。所以如果我给了他们不同的id,它就不能匹配正确的图像。 – iosfreak

回答

0

如果您有少于10张图像,然后给i个按钮的标签i*10+image.tag。然后您可以通过button.tag % 10检索image.tag,并且按钮标签将是唯一的。您甚至可以通过int b = button.tag/10检索仅按钮信息。

此外,您可以使用button.backgroundImage.tag访问该按钮的图像标签,这样该按钮可以有自己的独立标签系统,具体取决于您的使用。

+0

我想过这个。但我有一个无尽的滚动类型的东西,所以当你到达底部,它加载更多。一个想法是给ID一个十进制数,但它只是想将其舍入 – iosfreak

+0

如何为backgroudImage设置标签? – iosfreak

+0

'button.backgroundImage'是'UIImage'的一个实例,所以它有'tag'属性。只要'button.backgroundImage.tag = 0;' – PengOne