I have an app that lets the users chose one and only one button from four buttons.
the buttons are tied to one IBAction that takes id sender as a param. while each button has a tag. When the user clicks on any button text on that button is changed to @"*" and checks for the other buttons to erase any previous selection.
I used this code.
selectedType = b.tag;
NSLog(@"%d", selectedType);
switch (selectedType) {
case 1:
[btn2 setTitle:@"" forState:UIControlStateNormal];
[btn3 setTitle:@"" forState:UIControlStateNormal];
[btn4 setTitle:@"" forState:UIControlStateNormal];
break;
case 2:
[btn1 setTitle:@"" forState:UIControlStateNormal];
[btn3 setTitle:@"" forState:UIControlStateNormal];
[btn4 setTitle:@"" forState:UIControlStateNormal];
break;
case 3:
[btn1 setTitle:@"" forState:UIControlStateNormal];
[btn2 setTitle:@"" forState:UIControlStateNormal];
[btn4 setTitle:@"" forState:UIControlStateNormal];
break;
case 4:
[btn1 setTitle:@"" forState:UIControlStateNormal];
[btn2 setTitle:@"" forState:UIControlStateNormal];
[btn3 setTitle:@"" forState:UIControlStateNormal];
break;
default:
break;
}
[b setTitle:@"*" forState:UIControlStateNormal];
This is working right straight, but is there an easier technique?
If the user is only allowed to select one button then you could store which button is currently selected and when the user selects a new button, first deselect the old button and then select the new button and update the pointer to the selected button.
The same approach is still viable if the user is allowed to select multiple buttons though you would need to keep references to all the selected buttons.