• 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: how to pass arguments to NSMenu selector ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to pass arguments to NSMenu selector ?


  • Subject: Re: how to pass arguments to NSMenu selector ?
  • From: "Jack Nutting" <email@hidden>
  • Date: Thu, 12 Jun 2008 15:46:46 +0200

On Thu, Jun 12, 2008 at 3:00 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?

First of all, this list is for Xcode discussion, but your question is
a Cocoa question.  In the future please direct this to the appropriate
list.

That being said, the problem is that there is no connection between
the menu items you are creating and the URLs you are creating.  You
need a way to look up the URL based on the sender.  This is typically
done by setting an integer "tag" on the menu item, saving the URLs in
a structure based on the tag, and looking them up at the relevant
time.  Something like this:

// define urls as an NSMutableArray in your .h, and create it like
this in your -init:
urls = [[NSMutableArray alloc] init];
// and of course release it in -dealloc

nodes = array of NSXMLdocument ...
for(i=0;i<[nodes count];i++) {
  // keep these as local variables like this, rather than as instance
variables as you said in your followup post
  NSString *nodeTitle = ...;
  NSURL * nodeURL = ...;
  [urls insertObject:nodeURL atIndex:i];
  NSMenuItem *mi = [menu insertItemWithTitle:nodeTitle
action:@selector(connectNode:)
keyEquivalent:@"" atIndex:i];
  [mi setTag:i];
  [mi setTarget:self];
}
-(void)connectNode:(id)sender
{
  [[NSWorkspace sharedWorkspace] openURL:[urls objectAtIndex:[sender tag]];
}

--
// jack
// http://www.nuthole.com
 _______________________________________________
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

References: 
 >how to pass arguments to NSMenu selector ? (From: Dharmendra <email@hidden>)

  • Prev by Date: Re: how to pass arguments to NSMenu selector ?
  • Next by Date: Re: how to pass arguments to NSMenu selector ?
  • Previous by thread: Re: how to pass arguments to NSMenu selector ?
  • Next by thread: Re: how to pass arguments to NSMenu selector ?
  • Index(es):
    • Date
    • Thread