• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: MVC, sender, et al (was: Simple question: How do you store a sender's ID?)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.


References: 
 >MVC, sender, et al (was: Simple question: How do you store a sender's ID?) (From: Ondra Cada <email@hidden>)

  • Prev by Date: Re: Passing C Style Function Callbacks in Cocoa
  • Next by Date: Input Server?
  • Previous by thread: MVC, sender, et al (was: Simple question: How do you store a sender's ID?)
  • Next by thread: Re: MVC, sender, et al (was: Simple question: How do you store a sender's ID?)
  • Index(es):
    • Date
    • Thread