虽然我不确定这是否是您真正想要做的,但不久前我实现了一个类似的功能。就我而言,它仅适用于文件,但也适用于您的情况(或至少向您展示一种方法)
void populateRecentList(const char** files)
{
NSMenu* fileMenu = [[[NSApp mainMenu] itemWithTitle:@"File"] submenu];
NSMenu* recentItems = [[fileMenu itemWithTitle:@"Recent Files"] submenu];
[recentItems removeAllItems];
for (int i = 0; i < 4; ++i)
{
const char* filename = files[i];
NSString* name = [NSString stringWithUTF8String: filename];
NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:name action:@selector(onRecentFile:) keyEquivalent:@""];
[newItem setTag:i];
[newItem setRepresentedObject:[NSString stringWithFormat:@"%d",i]];
[newItem setKeyEquivalentModifierMask: NSCommandKeyMask];
[newItem setKeyEquivalent:[NSString stringWithFormat:@"%d",i + 1]];
[recentItems addItem:newItem];
[newItem release];
}
}