Hey Gary! I usually choose similar approach, though I prefer using integer constants instead of 'raw' numbers. Here's an example:
const int VIEW_TAG_A_SWITCH = 1;
const int VIEW_TAG_OTHER_SWITCH = 2;
- (IBAction)switchChanged:(UISwitch*)senderSwitch {
switch (senderSwitch.tag) {
case VIEW_TAG_A_SWITCH:
// do something
break;
case VIEW_TAG_OTHER_SWITCH:
// do something else
break;
}
}
This makes code more informational and help you to deal with situations when you need to change the UIView tag value for some reason (this way you only change it once in your nib and once in your code).
It's also very handy to use enums when you need to work with a group of controls. Here's a dummy single selection group of buttons example (something similar to what <input type="option" />
):
enum GenderOptionButtons {
kMaleGenderOptionButton = 10,
kFemaleGenderOptionButton,
kUndefinedGenderOptionButton,
NUM_GENDER_OPTION_BUTTONS
}
- (IBAction)buttonDidTouchUpInside:(UIButton *)senderButton {
for (int i = kMaleGenderOptionButton; i < NUM_GENDER_OPTION_BUTTONS; i ++) {
[(UIButton *)[self.view viewWithTag:i] setSelected:NO];
}
senderButton.selected = YES;
}