简体   繁体   English

如何在Xcode中复制菜单项?

[英]How can I copy menu items in Xcode?

So I'm making a web browsing app in Xcode for OS X, and right now I am working on history. 因此,我正在使用Xcode开发适用于OS X的网络浏览应用程序,现在我正在研究历史记录。 I have a menu called history in my MainMenu.xib, and I was wondering if it would be possible to add a menu item (through coding) to that every time the user loads a new page. 我的MainMenu.xib中有一个名为“历史记录”的菜单,我想知道是否有可能在每次用户加载新页面时在菜单项中添加一个菜单项(通过编码)。 Any help would be great. 任何帮助都会很棒。

Something like this should work: 这样的事情应该起作用:

- (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];
}

In your action you could then retrieve the URL (or some other object) that you set as the representedObject before: 在你的行动,你可以再检索您设置为URL(或其他物体) representedObject之前:

- (void)goToHistoryItem:(id)sender
{
    NSURL *historyURL = [sender representedObject];
    NSLog(@"history item selected: %@", historyURL);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM