雖然我不確定這是你真正想要做什麼,我不久前實現了一個類似的功能。對於我的情況它只是爲了文件,但也應該在你的情況下工作(或至少告訴你一種方法)
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];
}
}