2013-01-03 28 views
0

所以我在OS X中爲Xcode製作了網頁瀏覽應用程序,現在我正在研究歷史。我在MainMenu.xib中有一個名爲history的菜單,我想知道是否可以在每次用戶加載新頁面時添加一個菜單項(通過編碼)。任何幫助都會很棒。如何在Xcode中複製菜單項?

回答

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

在你的行動,你可以再檢索到您之前設置爲representedObject的URL(或其他物體):

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

謝謝!現在嘗試。 – biffletsbq

+0

什麼?它說ARC禁止[autorelease]。 – biffletsbq

+0

非常感謝!我一直在努力在過去的四天得到這個!很有幫助! – biffletsbq