Re: One IBAction, multiple results from multiple methods
Re: One IBAction, multiple results from multiple methods
- Subject: Re: One IBAction, multiple results from multiple methods
- From: Andy Lee <email@hidden>
- Date: Thu, 26 Feb 2009 13:30:06 -0500
On Feb 26, 2009, at 11:20 AM, Dave DeLong wrote:
Here's one for you:
I created a small app for the purpose of testing how to post
CGEvents and ended up building a keyboard in a window out of a whole
bunch of NSButtons. I didn't want to wire up each button to a
separate IBAction, because that would've been about 100 IBActions
all doing the same thing (the only difference being which keycode to
dispatch). So I created one IBAction, then hardcoded the keycode as
the tag of each button.
It would've been REALLY nice to put something like the following
into my .h file and have IB autodiscover it:
#define IBTag KEYCODE_A 0
#define IBTag KEYCODE_Z 6
...etc
And then have the tag field autocomplete those in IB. I think
anyone who would later look at my code would understand a heck of a
lot better what's going on. Yes, it's theoretically more work (to
do both the #defines and the tag hardcoding), but when you look at a
button in IB and see a tag of "42", I don't know anyone who thinks
"aha, that's a backslash". But if it were "KEYCODE_BACKSLASH_PIPE",
that would make a whole lot more sense. =)
I see how something like this could be convenient, so I'm not trying
to shoot the IBTag idea down, but in this example I wouldn't enter the
tags manually in IB. I'd put all 100 buttons in an NSView, have an
outlet to the NSView in my controller, and have awakeFromNib set up
all the buttons programmatically by looping through the NSView's
subviews. For each button it would set the target and action, and it
would set the tag by converting the button's title to a keycode
through a lookup table I'd hardcode. This would save me a lot of
Control-drags and tedious typing for each button -- I'd rather do the
tedious typing in code. I wouldn't worry so much about the meaning
(or existence) of the tag being obvious from inspecting a button in
IB. Some future person poking around should know to look in the code
when they see that none of the buttons have targets in IB.
--Andy
_______________________________________________
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