使用预设的动作选择器将项目添加到NSMenu [英] Adding item to NSMenu with action selector preset

查看:87
本文介绍了使用预设的动作选择器将项目添加到NSMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Obj-C/Cocoa编程的新手,尝试将菜单项动态添加到NSMenu实例并在插入时已经设置了项操作选择器时遇到问题.

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.

下面的第一行是我用来添加菜单项的代码行.您可以设置将动作(SEL)aSelector设置为"openEchowavesURL"功能.

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
}

推荐答案

如果要针对对象触发操作,则必须为新的

If you want the action to be triggered against your object, you have to specify a target for the new NSMenuItem:

NSMenuItem *item = [statusMenu insertItemWithTitle:[NSString stringWithFormat:@"%d - %@", convo.newMessagesCount, convo.ewName] action:@selector(openEchowavesURL:) keyEquivalent:@"" atIndex:0];
[item setTarget:self]; // or whatever target you want

如果您不这样做,请 NSResponder 链将被遍历,直到对象响应选择器为止.

If you don't do it, then the NSResponder chain will be walked until an object responds to the selector.

这篇关于使用预设的动作选择器将项目添加到NSMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆