Re: UIButton's Sender Control Events
Re: UIButton's Sender Control Events
- Subject: Re: UIButton's Sender Control Events
- From: Graham Cox <email@hidden>
- Date: Mon, 25 Jan 2010 10:22:41 +1100
On 25/01/2010, at 10:12 AM, Chunk 1978 wrote:
> so it's considered better to have the following:
>
> - (IBAction)button1
> {
> //button 1 action
> }
>
> - (IBAction)button2
> {
> //button 2 action
> }
>
> - (IBAction)button3
> {
> //button 3 action
> }
> .
>
> than to have the following:
>
> - (IBAction)buttons:(id)sender
> {
> switch ([sender tag])
> case 1: //button 1 action; break;
> case 2: //button 2 action; break;
> case 3: //button 3 action; break;
> }
>
> i would find it hard to believe that the former solution is both
> easier to debug and maintain than the later.
You might, but everyone else would most likely disagree.
It's far simpler and straightforward to simply wire the button to a single action than to have to also go in and set its tag to some arbitrary "magic number" that only your code knows how to interpret, whereas the separate methods can be clearly named and are visible in IB.
Switch statements are, as Andy mentioned, a bit of a code smell in OOP programs. They have their place, but this is definitely not one of them. Tags also have their place, but distinguishing totally separate buttons is not really a very good use for them - since the buttons already are distinguished by their pointers, why duplicate that by some additional number that effectively means the same thing?
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden