@selector and other class (Objective-C)

微笑、不失礼 提交于 2019-12-14 01:32:29

问题


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:


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


来源:https://stackoverflow.com/questions/3206623/selector-and-other-class-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!