• 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: Dharmendra <email@hidden>
  • Date: Thu, 12 Jun 2008 22:02:20 +0530

Thanks Jack and Sherm for helping me out as I tried to search the web on this issue, but couldn't find a crisp solution like this. With the messages archived at apple and cocoabuilder.com, it would be useful to others as well.  I used the setRepresentedObject solution and  I have got my 'First Obj-C/Cocoa/Xcode App' up and running! Now I have to optimize it for memory leakages etc. 

I made nodeURL local variable like 
NSURL *nodeURL = [[[NSURL aloc] initWithString:....] autorelease];

but have kept all other variables global as this part of the code itself is within a selector of an NSTimer called from awakeFromNib and that's all the code.  The timer repeats every minute or so. So, in a day it would be called 1440 times and if someone keeps this menulet running for a week, it would be 10000 times. The reason I am explaining this is I still have to figure out how to use dealloc. I have followed the procedure described at MacTech tutorial on Menulet but i get an error that dealloc is not declared and first time used, when it is defined on top. Most of the online codes i have seen has [super dealloc] used within a -dealloc function, but not called explicitly, so is it automaticalled called on meeting certain criteria ?


On Thu, Jun 12, 2008 at 7:22 PM, Sherm Pendley <email@hidden> wrote:
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

  • Follow-Ups:
    • Re: how to pass arguments to NSMenu selector ?
      • From: "Sherm Pendley" <email@hidden>
References: 
 >how to pass arguments to NSMenu selector ? (From: Dharmendra <email@hidden>)
 >Re: how to pass arguments to NSMenu selector ? (From: "Sherm Pendley" <email@hidden>)

  • Prev by Date: Re: Crash in virtual method call
  • Next by Date: Re: Compiler error when handling C++ std::string class variable
  • 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