2011-07-03 7 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]; 
} 

は、これを行うより良い方法はありますか?

おかげで、あなたは10個の未満のイメージがある場合 Coulton

+0

なぜ各ボタンにユニークなタグを付けないのですか? – PengOne

+0

これはすべてサーバーから行われ、各イメージは一意のIDを取得します。それぞれに1つのボタンがあります。だから私は彼らに異なるIDを与えた場合、それは正しい画像と一致しません。 – iosfreak

答えて

0

は、その後、i番目のボタンをタグi*10+image.tagを与えます。次に、image.tagbutton.tag % 10で取得できます。ボタンタグは一意になります。 int b = button.tag/10によってボタンのみの情報を取得することもできます。

また、ボタンの画像のタグにはbutton.backgroundImage.tagでアクセスできるため、ボタンには用途に応じて独自のタグ付けシステムを設定することができます。

+0

私はそれについて考えました。しかし、私は無限のスクロールタイプのものを持っています。 1つのアイデアはIDに小数点を与えていましたが、丸めたいだけです – iosfreak

+0

backgroudImageのタグを設定するにはどうすればよいですか? – iosfreak

+0

'button.backgroundImage'は' UIImage'のインスタンスなので、 'tag'プロパティを持っています。 'button.backgroundImage.tag = 0;' – PengOne

関連する問題