Re: How to set menu action with argument.
Re: How to set menu action with argument.
- Subject: Re: How to set menu action with argument.
- From: Greg Titus <email@hidden>
- Date: Wed, 4 Jul 2001 21:12:42 -0700
On Wednesday, July 4, 2001, at 08:14 PM, Youngjin Kim wrote:
Let's say this method will display a picture with given id.
-(void)selectPicture:(int)picID{
...
}
and I want the picture by selecting menu which has to be build in
runtime. I tried something like this.
// make a menu item and set action & target.
...
[newItem setTarget:self];
[newItem setAction:@selector(selectPicture:)];
Obviously, this code doesn't work because it doesn't specify image id.
How can I set this menu item's action to call selector _with_
argument(s)?
Short answer: You can't directly do it that way.
Longer answer: What you really want to do is identify which particular
menu item was selected even though they all call the same action method.
You can do this by setting each menu item up with an appropriate 'tag'
or a 'representedObject' and then asking the sender of the action for
that value back.
So your code would look like:
- (void)selectPicture:(id)sender
{
int picID = [sender tag];
...
}
// make a menu item and set action & target
...
[newItem setTarget:self];
[newItem setAction:@selector(selectPicture:)];
[newItem setTag:somePicID];
Hope this helps,
--Greg