Single IBAction for multiple UIButtons versus single IBAction for single UIButton
- by Miraaj
While using story-board there are two different approaches which my team mates follow:
Approach 1: To bind unique action with each button, ie:
Done button - binded to - doneButtonAction
Cancel button - binded to - cancelButtonAction
OR
Approach 2: To bind single action to multiple buttons, ie:
Done button - binded to - commonButtonAction
Cancel button - binded to - commonButtonAction
Then in commonButtonAction they prefer to use switch case like this:
- (IBAction)commonButtonAction:(id)sender
{
UIButton *button = (UIButton *)sender;
switch (button.tag) {
case 201: // done button
[self doneButtonAction:sender];
break;
case 202: // cancel button
[self cancelButtonAction:sender];
break;
default:
break;
}
}
- (void)cancelButtonAction:(id)sender
{
// no interesting stuff, simple dismiss of view :-(
}
- (void)doneButtonAction:(id)sender
{
// some interesting stuff ;-)
}
Reasoning which they give to follow approach 2 is - in each view controller during code walk through anyone can easily identify where to find code related to button actions.
While others discard this idea because they say that adding an extra switch case is unnecessary and is not a common practice.
What are your views?