使用动作选择器预设将项目添加到 NSMenu
我是 Obj-C/Cocoa 编程的新手,在尝试将菜单项动态添加到 NSMenu 实例并在插入时设置项目操作选择器时遇到问题。
我可以很好地添加动态菜单项,但是当通过菜单单击该项目时,操作选择器不会触发。
下面的第一行是我用来添加菜单项的代码行。您可以将“我正在设置操作:(SEL)aSelector”设置为“openEchowavesURL”函数。
该函数位于同一个控制器类文件中,我在下面包含了函数定义。
我只是调用了错误的选择器语法还是其他什么?或者让菜单项在单击时调用选择器的技巧是什么?
[statusMenu insertItemWithTitle:[NSString stringWithFormat:@"%d - %@", convo.newMessagesCount, convo.ewName] action:@selector(openEchowavesURL:) keyEquivalent:@"" atIndex:0];
- (void)openEchowavesURL:(id)sender {
// function details here
}
I'm new to Obj-C/Cocoa programming, and I'm having an issue trying to dynamically add menu items to an NSMenu instance and have the items action selector already set upon insertion.
I can, add the dynamic menu items fine, but the action selector doesn't trigger when the item is clicked via the menu.
The first line below is the line of code I am using to add the menu item. You can set I'm setting the action:(SEL)aSelector to the "openEchowavesURL" function.
This function is in the same controller class file and I've included the function definition below.
Am I just calling the wrong selector syntax or someting? Or what is the trick to get the menu item to call a selector when clicked?
[statusMenu insertItemWithTitle:[NSString stringWithFormat:@"%d - %@", convo.newMessagesCount, convo.ewName] action:@selector(openEchowavesURL:) keyEquivalent:@"" atIndex:0];
- (void)openEchowavesURL:(id)sender {
// function details here
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望针对对象触发操作,则必须为新的 NSMenuItem:
如果您不这样做,则 NSResponder 链将被遍历,直到对象响应选择器。
If you want the action to be triggered against your object, you have to specify a target for the new NSMenuItem:
If you don't do it, then the NSResponder chain will be walked until an object responds to the selector.
操作需要一个目标,否则它们会被发送为零,然后应用新规则。
Actions need a target or else they get sent to nil and then new rules apply.