私は、グループ化された各画像に対して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];
}
は、これを行うより良い方法はありますか?
おかげで、あなたは10個の未満のイメージがある場合 Coulton
なぜ各ボタンにユニークなタグを付けないのですか? – PengOne
これはすべてサーバーから行われ、各イメージは一意のIDを取得します。それぞれに1つのボタンがあります。だから私は彼らに異なるIDを与えた場合、それは正しい画像と一致しません。 – iosfreak