-(void)setButtons_AsPerTheMatrixSelection
{
int x = 7;
int y = 60;
for (int j = 0; j <= 35; ++j)
{
if (j <= 5)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
else if (j > 5 && j <= 11)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-306, y+51, width, height)];
else if (j > 11 && j <= 17)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-612, y+102, width, height)];
else if (j > 17 && j <= 23)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-918, y+153, width, height)];
else if (j > 23 && j <= 29)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-1224, y+204, width, height)];
else if (j > 29 && j <= 35)
btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x-1530, y+255, width, height)];
btnMatrix.tag = j;
[btnMatrix setBackgroundImage:imgDefaultBG forState:UIControlStateNormal];
btnMatrix.userInteractionEnabled = TRUE;
[btnMatrix addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
[viewWithButtons addSubview:btnMatrix];
[self.view addSubview:viewWithButtons];
x = x + 51;
y = y;
[arrButton addObject:btnMatrix];
}
}
これは、6 * 6 のマトリックスに従って動的ボタンを追加する私のコードです
ここにボタンタグも追加しています。
ボタンをクリックして、このコードで背景画像を変更したいと思います...
-(void)changeImage:(id)送信者
{
UIImage *img = [UIImage imageNamed:@"No.png"];
UIButton *tmpButton = (UIButton *)[self.view viewWithTag:btnMatrix.tag];
NSLog(@"btn.tag is... %d",tmpButton.tag);
[btnMatrix setBackgroundImage:img forState:UIControlStateNormal];
}
ボタンをクリックすると、最後にタグ付けされたボタンの画像が常に変更されます...
選択したボタン自体の背景を変更する必要があります。
どうすればそれが可能になりますか。
私が間違いをしている部分が得られないことを教えてください。
ありがとう。