UIButtons是动态的,不会根据所选按钮的标签正确更改其背景图像。
问题描述:
- (无效)setButtons_AsPerTheMatrixSelectionUIButtons是动态的,不会根据所选按钮的标签正确更改其背景图像。
{
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
这里我加入该按钮的矩阵标签也
现在我要点击按钮&想改变黑色与此代码一轮的图片
- (空)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];
}
当我点击任何按钮,它总是在不断变化的图像最后一个标记的按钮...
我必须更改所选按钮本身的背景。
怎么可能。
请指导我,我没有得到一个地方,我犯了一个错误。
谢谢。
答
-(void)changeImage:(UIButton*)sender
{
UIImage *img = [UIImage imageNamed:@"No.png"];
[sender setBackgroundImage:img forState:UIControlStateNormal];
}
顺便说一句,你的代码是太乱了,你可以做这样的
int pw = 306;
int ph = 51;
int ch = 6;
int cv = 6;
for (int i = 0 ; i < cv ; ++i)
{
for (int j = 0 ; j < ch ; ++j)
{
UIButton *btnMatrix = [[[UIButton alloc] initWithFrame:CGRectMake(7+pw*j, 51+ph*i, width, height)] autorelease];
btnMatrix.tag = i*ch+j;
}
}
答
尝试在该方法这一变化:
-(void)changeImage:(id)sender
{
UIButton *Btnmatrix = (UIButton *)sender
UIImage *img = [UIImage imageNamed:@"No.png"];
[Btnmatrix setBackgroundImage:img forState:UIControlStateNormal];
}
答
在功能
-(void)setButtons_AsPerTheMatrixSelection
让你的按钮本地化如下
for (int j = 0; j <= 35; ++j)
{
if (j <= 5){
UIButton *btnMatrix = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
btnMatrix.tag = j;
[btnMatrix setBackgroundImage:imgDefaultBG forState:UIControlStateNormal];
btnMatrix.userInteractionEnabled = TRUE;
[btnMatrix addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
[viewWithButtons addSubview:btnMatrix];
}
// and so on every button
}
希望这会对你有所帮助。对不起,我的英语不好:)
答
在我看来,像btnMatrix
是伊娃,因为你先后将其指定为您增加每个按钮,它会坚持你的最后一个按钮的引用您完成创建按钮后。更改线路检查sender
对象,而不是:
UIButton *tmpButton = (UIButton*)sender.tag;
谢谢,我现在可以改变每一个选择按钮的图像将按照所选......但在最后一行(指6个按键最后一行)我无法改变图像。 –
是否打印日志?可能该事件没有被解雇,可能是按钮不在父视图 – adali
它现在工作正常.....谢谢\ –