0

これを使用して、ボタンのグリッドを定義しています。次のように、特定のボタンの属性をプログラムで変更したい:

if(button.tag == 6)
{
    [button setBackgroundImage:imageRed forState:UIControlStateNormal];
    [button setImage:imageRed forState:UIControlStateNormal];
}

以下ボタン作成。


for (int y=0; y < 3; y++) {
    for (int x = 0; x < 3; x++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        //button.frame = CGRectMake(40 + 80 * x, 40 + 80 * y, 80, 80);

        button.frame=CGRectMake(5+5*x,5+5*y,5,5);

        unsigned buttonNumber = y * 3 + x + 1;
        button.tag = buttonNumber;
        //[button setTitle:[NSString stringWithFormat:@"%u", buttonNumber] forState:UIControlStateNormal];
        [button setBackgroundImage:imageWhite forState:UIControlStateNormal];
        [button setImage:imageWhite forState:UIControlStateNormal];

        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview: button];
    }
}
4

2 に答える 2

1

あなたの質問が何であるか完全にはわかりませんが、「特定のタグを持つビューを見つけるにはどうすればよいですか?」のようです。答えはUIViewメソッドです

- (UIView *)viewWithTag:(NSInteger)tag

これにより、現在のビューと、指定されたタグのすべてのサブビューが調べられ、それに一致する最初のビューが返されます。たとえば、タグ6のボタンを取得するには、次のようにします。

UIView* button = [self.view viewWithTag:6];

もちろん、メソッドなどのターゲットメソッドを処理する場合buttonPressed:、イベントをトリガーしたビューはパラメーターとして渡されるため、それが必要な場合にのみ使用することもできます。それ以外は、コード内のすべてが正常に見えます。

于 2012-09-23T02:59:05.823 に答える
0

結局これを使ってしまった…

 for (int row = 0; row < ROWS; row++) {
    for (int col = 0; col < COLS; col++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       // [ButtonArray addObject:button];

        button.tag = row * COLS + col;

        button.frame = CGRectMake(5+5*row,5+5*col,5,5);
        [button addTarget:self action:@selector(didPushButton:) forControlEvents:UIControlEventTouchDown];

        [button setBackgroundImage:imageWhite forState:UIControlStateNormal];
        [button setImage:imageWhite forState:UIControlStateNormal];
        [self.view addSubview:button];
        buttonArr[row*COLS+col]=button;
    }
}
于 2012-09-23T19:39:14.450 に答える