Re: MVC, sender, et al (was: Simple question: How do you store a sender's ID?)
Re: MVC, sender, et al (was: Simple question: How do you store a sender's ID?)
- Subject: Re: MVC, sender, et al (was: Simple question: How do you store a sender's ID?)
- From: Andy Lee <email@hidden>
- Date: Fri, 9 Apr 2004 10:15:31 -0400
On Apr 9, 2004, at 5:27 AM, Ondra Cada wrote:
Apropos of this: I've often wondered, what's the best style in case
you bind something in IB to an outlet and to an action both:
@interface Controller ... {
IBOutlet widget;
}
IBAction widgetAction:sender { ... }
In the Controller implementation this means that either I use two
different ways in one piece of source to address one object ("sender"
in the action, "widget" elsewhere), or that I use "widget" in the
action. Neither of them feels quite correct to me.
Well, for years I use the former without the slightest problem -- but
still, I don't quite like that. Any ideas? Am I just a fool to be
concerned with that, or do I overlook something obvious all those
years? ;)
I'd use "widget" rather than "sender" except in certain special cases.
I prefer to use the more strongly typed (and more precisely named)
variable. This helps telegraph my intentions to the reader of my code,
and it provides (a little) protection via compiler type-checking.
@interface Controller ... {
IBOutlet Widget *widget;
}
- (void) tweakWidget: (Widget *) widget;
...
- (IBAction) widgetAction: (id) sender {
[widget doWidgetThing];
[self tweakWidget:widget];
}
Strictly speaking, I think of action methods as advertising a behavior
that is open to *any* object being passed as the "sender" argument,
where by convention, sender is the conceptual requester of the
operation (not necessarily the actual sender of the -widgetAction:
message). Often I know (because I made it so) that I can make certain
simplifying assumptions, but not always. I may really want to do my
business with widget no matter who "sender" is, but sometimes I may
need to do this:
- (IBAction) widgetAction: (id) sender {
if (sender == widget) {
[widget doWidgetThing];
[self tweakWidget:widget];
} else {
// ...
}
}
Just my two cents, of course.
--Andy
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.