0

oldTag 按钮没有变回白色??我错过了什么?

谢谢

-(void) myCharValue: (UIButton*)btn
{

    [btn setBackgroundColor:[UIColor redColor]]; //current tag_button pressed

    UIButton *btnTemp;
    btnTemp = (UIButton*)[btnChar viewWithTag:oldTag];
    [btnTemp setBackgroundColor:[UIColor whiteColor]]; // <--- oldTag button is not changing back to white ??

    oldTag = btn.tag;
}


//setup
for (int i=0; i<16; i++) {
        for (int j=0; j<16; j++) {
            btnChar = [UIButton buttonWithType:UIButtonTypeCustom ];
            [btnChar setFrame:CGRectMake(j*40+1, i*40+1, 38, 38)];
            [btnChar setBackgroundColor:[UIColor whiteColor]];
            [btnChar setTitle:[NSString stringWithFormat:@"%c", k]  forState:UIControlStateNormal ];
            [btnChar setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btnChar addTarget:self action:@selector(myCharValue:) forControlEvents:UIControlEventTouchUpInside];
            [btnChar setTag:k];
            [svDisplay addSubview:btnChar];
            k++;
        }
    }
4

2 回答 2

2

我只想补充一点,tag属性的默认值是 0,所以如果你从 0 开始循环,第一个按钮在技术上不会被标记。稍后,当您viewWithTag:从(chech @jrtc27 的答案)请求 0时,您将在该数组svDisplay中获得第一个视图。[svDisplay subviews]

于 2012-05-06T09:03:54.227 回答
1

您的按钮是 的子视图svDisplay,而不是btnChar,因此该行btnTemp = (UIButton*)[btnChar viewWithTag:oldTag];应该是btnTemp = (UIButton*)[svDisplay viewWithTag:oldTag];

于 2012-05-06T08:50:20.690 回答