@selector和其他类(Objective-C) [英] @selector and other class (Objective-C)
问题描述
在对象内部,我使用NSMenu的addItemWithTitle:action:keyEquivalent:
创建NSMenuItems.问题是我希望将另一个对象上的方法作为操作调用. action:
部分采用@selector
作为参数,我不知道如何使用它来调用其他对象上的方法.我可以在创建NSMenu的对象内部创建一个方法,然后从该对象调用我想在另一个对象上调用的方法.但是然后,我不知道该对象的任何良好命名约定.
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.
推荐答案
使用 Objectvive-C编程语言的示例:选择器,它对表格单元格执行类似的操作:
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];
这篇关于@selector和其他类(Objective-C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!