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)送信者

{

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

このメソッドでこの変更を試してください:

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

私にはivarのように思えbtnMatrixます。ボタンを追加するたびに各ボタンに連続して割り当てるため、ボタンの作成が完了した後、最後のボタンへの参照が保持されます。sender代わりにオブジェクトを検査するように行を変更します。

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