@selector and other class (Objective-C)
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.
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];
精彩评论