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: "Sherm Pendley" <email@hidden>
- Date: Thu, 12 Jun 2008 09:52:24 -0400
On Thu, Jun 12, 2008 at 9:00 AM, 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
Why would you assume that? Your code doesn't do anything to associate
the value of nodeURL with a particular menu item. All it does is
create a nodeURL object that's an instance variable of your controller
object.
> That is because the selector is different from a typical function.
What? You're not only barking up the wrong tree, you're in the wrong
*forest*. A selector is not a function, typical or otherwise. Nor is
it a method. A selector is the name of a message, and if there were
something wrong with it your action method wouldn't be called *at
all*.
> Now, how do you pass arguments to selectors
You don't. Action methods are passed one argument, typically named
"sender" as in your code, which is a reference to the GUI element that
sent the action.
> when you want to display dynamic information in menubar?
What you want to do is tell each menu item what object it represents:
[[menu itemAtIndex: i] setRepresentedObject: nodeURL];
Then, in your action method, use the "sender" argument to fetch the
object that's represented by the clicked-on menu item:
[[NSWorkspace sharedWorkspace] openURL: [sender representedObject]];
Also, remove the instance variable declarations from your header file
- declaring them inside the for() loop is correct in this case. Also,
you mentioned retaining nodeURL - don't do that. If the menu item
needs to retain anything, it will do so itself.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
_______________________________________________
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