麻烦" viewWithTag"

时间:2012-05-06 00:46:22

标签: objective-c cocoa-touch

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

THX

-(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++;
        }
    }

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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