-(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)sender
{
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];
}
当我单击任何按钮时,它总是会更改最后一个标记按钮的图像...
我必须更改所选按钮本身的背景。
怎么可能。
请指导我,我没有得到我做错的部分。
谢谢。