我个人不喜欢ib,所以我建议以编程的方式来做!
使用nsarray存储uibutton的索引。
row*COLUMNS+column
.
将tag属性设置为base+index(base为任意值>0),以便找到按钮的位置:
index=tag-BASE; row=index/COLUMNS; column=index%COLUMNS;
- (void)loadView {
[super loadView];
for (NSInteger row = 0; row < ROWS; row++) {
for (NSInteger col = 0; col < COLS; col++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonArray addObject:button];
button.tag = BASE + row * COLS + col;
button.frame = ...;
[button addTarget:self action:@selector(didPushButton:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
}
}
}
- (void)didPushButton:(id)sender {
UIButton *button = (UIButton *)sender;
NSInteger index = button.tag - BASE;
NSInteger row = index / COLS;
NSInteger col = index % COLS;
// ...
}