Re: how to pass arguments to NSMenu selector ?
Re: how to pass arguments to NSMenu selector ?
- Subject: Re: how to pass arguments to NSMenu selector ?
- From: Andy Lee <email@hidden>
- Date: Thu, 12 Jun 2008 09:50:52 -0400
As you've discovered, each menu item only operates on information you
specifically assigned to it. It has no way of remembering what
nodeURL was at the time you created the menu item.
I see Jack Nutting has just posted one solution. Here is another.
NSMenuItem has a representedObject that you can use for your own
purposes. I would change your code like this:
NSMenuItem *menuItem = [menu insertItemWithTitle:nodeTitle
action:@selector(connectNode:) keyEquivalent:@"" atIndex:i];
[menuItem setTarget:self];
[menuItem setRepresentedObject:nodeURL];
Then:
-(void)connectNode:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[sender representedObject]];
}
This uses the fact that sender will be the NSMenuItem that "sends" the
connectNode: message (hence the name "sender").
And yes, this was the wrong list -- didn't notice that until Jack
mentioned it.
--Andy
On Jun 12, 2008, at 9:24 AM, Dharmendra wrote:
Just a correction that nodeTitle and nodeURL along with other
variables are defined in the header file and only instantiated in
the implementation file. So actual code is like this :
nodeTitle = [NSString ...;
nodeURL = [NSURL ... retain];
On Thu, Jun 12, 2008 at 6:30 PM, Dharmendra <email@hidden>
wrote:
I have a dynamic menu items generated based on some online
information. Next I want is to link them to their respective online
info pages. So I tried to use the following procedure, but it doesn'
work :
nodes = array of NSXMLdocument ...
for(i=0;i<[nodes count];i++) {
NSString *nodeTitle = ...;
NSURL * nodeURL = ...;
[menu insertItemWithTitle:nodeTitle action:@selector(connectNode:)
keyEquivalent:@"" atIndex:i]; [[menu itemAtIndex:i] setTarget:self];
}
-(void)connectNode:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:nodeURL];
}
My assumption was that nodeURL used in connectNode selector would
be unique for each calls, but it uses the last assigned value of
nodeURL for all menu items. That is because the selector is
different from a typical function.
Now, how do you pass arguments to selectors when you want to
display dynamic information in menubar?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden