Does anybody know how to set the title of each individual button in an array of buttons using an IBOutletCollection? This is what I have tried, but the code sets the title of all the buttons. I have connected the outlet to the buttons and set their respective tags.
.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];
}
}
If you only want to set the title of the button you've clicked on, get rid of the for loop,
- (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];
}