NSMenuItem的选择器放在哪里 [英] Where to put selector of NSMenuItem
问题描述
我尝试了解可可粉中的几件事,但我被一件事困住了.我正在追踪简约可可编程,它是NSMenuItem
负责终止应用程序.现在,我想创建另一个带有快捷方式的NSMenuItem
,当按下它时,它会NSLog
-s.但是我不知道该把选择器的实现放在哪里?我应该将整个NSApplication
子类化吗?我应该setDelegate
到某个NSObject
实例,充当控制器吗?
I try to understand a few things in Cocoa but I got stuck on one thing. I'm following Minimalistic Cocoa Programming, there it is a NSMenuItem
responsible for terminating the app. Now, I would like to create another NSMenuItem
, with a shortcut, that when pressed, it NSLog
-s something. But I don't know where should I put the implementation of said selector? Should I subclass the whole NSApplication
? Should I setDelegate
to some NSObject
instance, acting as a controller?
推荐答案
您需要创建一个可用作菜单项目标的类.像这样:
You need to create a class that can be used as the target of the menu item. Something like this:
@interface Tester : NSObject
@end
@implementation Tester
- (void)logTest:(id)sender
{
NSLog(@"Test");
}
@end
然后将其设置为目标:
id testMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Log Test" action:@selector(logTest:) keyEquivalent:@"l"] autorelease];
id tester = [[[Tester alloc] init] autorelease];
[testMenuItem setTarget:tester];
这篇关于NSMenuItem的选择器放在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!