Jennifer
Jennifer

Reputation: 33

@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.

Upvotes: 3

Views: 2886

Answers (1)

Franci Penov
Franci Penov

Reputation: 75981

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];  

Upvotes: 3

Related Questions