0

所以我在 Xcode 中为 OS X 制作一个网页浏览应用程序,现在我正在研究历史。我的 MainMenu.xib 中有一个名为 history 的菜单,我想知道是否可以在每次用户加载新页面时添加一个菜单项(通过编码)。任何帮助都会很棒。

4

1 回答 1

0

像这样的东西应该工作:

- (void)addHistoryItemWithTitle:(NSString *)title URL:(NSURL *)historyURL
    NSMenuItem *menuItem = [[[NSMenuItem alloc] initWithTitle:title action:@selector(goToHistoryItem:) keyEquivalent:@""] autorelease];
    menuItem.representedObject = historyURL;
    //Note: You would normally have an outlet for your History menu or use
    //      itemWithTag:, so that it works in localized versions of your app.
    NSMenuItem *historyMenuItem = [[NSApp mainMenu] itemWithTitle:@"History"];
    [[historyMenuItem submenu] addItem:menuItem];
}

然后,在您的操作中,您可以检索您之前设置的 URL(或其他对象)representedObject

- (void)goToHistoryItem:(id)sender
{
    NSURL *historyURL = [sender representedObject];
    NSLog(@"history item selected: %@", historyURL);
}
于 2013-01-03T22:18:03.273 回答