如何拥有具有动态操作的 NSMenu
我想创建一个 NSMenu,其选项类似于您在 Windows 资源管理器中找到的“发送到”选项,其中它将列出您可以将文件发送到的附加设备。
根据我的研究,似乎不可能定义一个向函数发送参数的选择器,因此这不是使用 @selector(@"sendToVolume:1")
的情况。那么,当项目数量未知时,我还能如何让菜单根据单击的项目执行不同的任务呢?
I want to create an NSMenu with an option similar to the Send To option you'd find in Windows Explorer where it will list the devices attached that you can send the file to.
From my research it seems that it's not possible to define a selector that sends a parameter to the function as well, so it's not a case of having @selector(@"sendToVolume:1")
. So how else could I have the menu perform a different task based on which item is clicked when the number of items is unknown?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSMenuItem 有一个representedObject 属性,可用于存储您想要的任何内容,例如对项目所代表的目的地的引用。
当选择器被调用时,您可以获取representedObject:
NSMenuItem has a
representedObject
property, which can be used to store anything you'd like, such as a reference to the destination that item represents.When the selector is invoked, you can then get the representedObject back:
但您可以使用带参数的选择器!
NSObject
定义了三个方法,如下所示:现在,第一个就像有
@selector(someMethod:)
,但最后两个用于向选择器发送参数。例如:那么你可以这样使用它:
But you can use selectors with parameters!
NSObject
has three methods defined like this:Now, the first is like having
@selector(someMethod:)
, but the last two are used to send parameters to the selector. For example:then you could use it like this: