Re: Using non-id sender in IBAction methods
Re: Using non-id sender in IBAction methods
- Subject: Re: Using non-id sender in IBAction methods
- From: Ken Thomases <email@hidden>
- Date: Sun, 7 Jun 2009 12:23:43 -0500
On Jun 7, 2009, at 10:45 AM, Marc Liyanage wrote:
With the new dot notation I sometimes use explicit types in my
IBAction methods:
- (IBAction)doSomething:(UIButton *)button ...
instead of
- (IBAction)doSomething:(id)sender ...
so I don't have to downcast "sender" to be able to use the dot
notation.
Do others do this too? Is this discouraged for some reason?
It's legitimate to do, but there's a trade-off: generality.
In theory, action methods may be invoked by a variety of types of
senders. The same action might be invoked by a menu item, a button, a
toolbar item, a text field, etc. You might not now anticipate the
future use of an action method.
So, you can do what you describe, but it does limit the potential
usefulness of the action methods you design.
On the other hand, many of the things you might do with the sender
will require that you know the type of the sender, so it might not
matter. That is, most truly general action methods will either not
make use of the sender or will invoke only very generic methods of the
sender (e.g. -tag). If you're writing an action method that really
gets into the specifics of its sender, then that action is probably
only meaningful with that particular type of sender, anyway.
In short, it's a design decision with no right answer. Understand the
trade-off and make whatever choice is appropriate for your application.
Cheers,
Ken
_______________________________________________
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