0

-(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];

}

当我单击任何按钮时,它总是会更改最后一个标记按钮的图像...

我必须更改所选按钮本身的背景。

怎么可能。

请指导我,我没有得到我做错的部分。

谢谢。

4

4 回答 4

3
-(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;
    }
}
于 2012-05-25T07:06:55.553 回答
1

在此方法中尝试此更改:

-(void)changeImage:(id)sender
{
   UIButton *Btnmatrix = (UIButton *)sender
   UIImage *img = [UIImage imageNamed:@"No.png"];
   [Btnmatrix setBackgroundImage:img forState:UIControlStateNormal];
}
于 2012-05-25T07:10:35.703 回答
1

在你的功能中

-(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

}

希望这会帮助你。对不起,我的英语不好 :)

于 2012-05-25T07:16:51.227 回答
1

在我看来,它似乎btnMatrix是一个 ivar,并且由于您在添加它们时将其连续分配给每个按钮,因此在您完成创建按钮后,它将保留对您最后一个按钮的引用。更改您的行以检查sender对象:

UIButton *tmpButton = (UIButton*)sender.tag;
于 2012-05-25T07:22:11.820 回答