views:

53

answers:

1

Hi,

Inside an object I use NSMenu's addItemWithTitle:action:keyEquivalent: to create NSMenuItems. The problem is that I wish to call a method on another object as action. The action: part takes an @selector as parameter and I don't know how to use this to call methods on other objects. I could create a method inside the object creating the NSMenu and then from that object I could call the method I would like to call on another object.. But then I don't know any good naming convention for that.

+1  A: 

Use setTarget: on the newly created NSMenuItem object to set the target object for the action message. Here's an example from The Objectvive-C Programming Language: Selectors, which does similar thing for a table cell:

[myButtonCell setAction:@selector(reapTheWind:)];  
[myButtonCell setTarget:anObject];  
Franci Penov
Thanks a million! Why didn't I think of that.
Jennifer
what is the target when it is not explicitly set?
hop
If the target is `nil` the application will go up the responder chain to find an object that responds to the action.
Wevah