有人知道如何使用IBOutletCollection设置按钮数组中每个按钮的标题吗?这是我尝试过的,但代码设置了所有按钮的标题。我已将插座连接到按钮并设置各自的标签。
.h file
@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttonCollection;
.m file
- (IBAction)switchAction:(id)sender {
for (UIButton *btn in buttonCollection) {
if (btn.tag == 0) {
[btn setTitle:@"1st Button" forState:UIControlStateNormal];
} else if (btn.tag == 1) {
[btn setTitle:@"2nd Button" forState:UIControlStateNormal];
} else if (btn.tag == 2) {
[btn setTitle:@"3rd Button" forState:UIControlStateNormal];
}
}
答案 0 :(得分:1)
如果你只想设置你点击的按钮的标题,摆脱for循环,
- (IBAction)switchAction:(UIButton *)sender {
if (sender.tag == 0) {
[sender setTitle:@"1st Button" forState:UIControlStateNormal];
} else if (sender.tag == 1) {
[sender setTitle:@"2nd Button" forState:UIControlStateNormal];
} else if (sender.tag == 2) {
[sender setTitle:@"3rd Button" forState:UIControlStateNormal];
}
答案 1 :(得分:0)
试试这个:
for (int i = 0; i < [buttonCollection count]; ++i)
{
[((UIButton*)self.buttonCollection[i]) setTitle: [NSString stringWithFormat: @"%d button", i] forState: UIControlStateNormal];
}