0

そのため、Xcode for OS XでWebブラウジングアプリを作成しており、現在は履歴に取り組んでいます。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];
}

representedObjectアクションでは、前に設定したURL(またはその他のオブジェクト)を取得できます。

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