我正在使用它来定义按钮网格。我想以编程方式更改特定按钮的属性,例如:
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];
}
}