如何以编程方式向NSMenuItem添加撤消操作?

时间:2017-07-19 23:33:01

标签: macos cocoa appkit

我正在以编程方式构建NSMenu而没有任何Nib 请看一下这段代码:

self.editMenu = [[NSMenu alloc] initWithTitle:@"Edit"];

[self.editMenu addItemWithTitle:NSLocalizedString(@"Undo", nil)
                         action:@selector(undo:)
                  keyEquivalent:@"z"];

[self.editMenu addItemWithTitle:NSLocalizedString(@"Redo", nil)
                         action:nil
                  keyEquivalent:@"Z"];

[self.editMenu addItem:[NSMenuItem separatorItem]];

[self.editMenu addItemWithTitle:NSLocalizedString(@"Cut", nil)
                         action:@selector(cut:)
                  keyEquivalent:@"x"];

[self.editMenu addItemWithTitle:NSLocalizedString(@"Copy", nil)
                         action:@selector(copy:)
                  keyEquivalent:@"c"];

[self.editMenu addItemWithTitle:NSLocalizedString(@"Paste", nil)
                         action:@selector(paste:)
                  keyEquivalent:@"v"];

如您所见,有@selector(cut:)@selector(copy:)@selector(paste:)。但是没有@selector(undo:)@selector(paste:)。编译器说“Undeclared selector”undo:'。

Undeclared selector undo:

如何以编程方式实现撤消和重做菜单项的操作?

0 个答案:

没有答案