我需要更改UIButtons矩阵上的图像,而我所知道的唯一解决按钮问题的是标签。但我找不到实际使用此标识符的方法。 这些按钮是在viewDidLoad期间以编程方式创建的。如何使用标签访问UIButton并更改其图像?
下面是用于创建按钮的代码:
#define N_ROWS 4
#define N_COLS 3
int N_IMG = 0;
for (int a = 0; a < N_COLS; a++) {
for (int j = 0; j < N_ROWS; j++) {
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(a * 65.0 + 25, j * 65.0 + 15, 10.0, 10.0);
aButton.tag = j + a * N_ROWS + 1;
[aButton setBackgroundColor:[UIColor redColor]];
N_IMG = N_IMG++;
[self.view addSubview:aButton];
number_sorted = 1;
}
}
这里是用于设置图像的代码:
- (IBAction)set_image:(id)sender {
#define N_ROWS 4
#define N_COLS 3
int N_IMG = 0;
for (int a = 0; a < N_COLS; a++) {
for (int j = 0; j < N_ROWS; j++) {
uibutton aButton.tag == (j + a * N_ROWS + 1)
setImage:[UIImage imageNamed:[puzzles objectAtIndex:N_IMG]]
forState:UIControlStateNormal];
N_IMG = N_IMG++;
}
}
}
这是代码,其中truble开始: 的UIButton aButton.tag ==(j + a * N_ROWS + 1)
我可以设置这项功能吗?