How to add programmatically add menus...
How to add programmatically add menus...
- Subject: How to add programmatically add menus...
- From: Michael Mulligan <email@hidden>
- Date: Wed, 16 Jan 2002 11:45:02 -0500
Hello all
In my application, I allow the user to save bookmarks of servers and
what not. I would like to have an item in my File menu ("Open
Bookmark") that would act as a submenu, presenting a list of bookmarks
that I read from a file. So far, I have been able to generate the menu
and populate it with the individual bookmarks from the file. Yay! :-)
However, I have not been able to put it in the File menu--rather, it is
it's own separate menu in the main menu bar (it comes after Help). I
tried to follow Apple's sample files ("Menu Madness" in particular), but
that only got me so far as to actually *generate* the menu, not to
insert it as a submenu where I want it in the menu bar.
I thought that I had figured it out when I thought I could use
itemWithTitle: to find the index of the insertion point in my main menu
and then insertItem: atIndex: to put it in, but no luck. I feel as if
I'm missing something very simple here, any suggestions?
Also, on a separate note, seeing how these bookmarks can be updated
during a session, I need to refresh the menu throughout use. I know
where I would call such a refresh method but was wondering if there is a
built in method to refresh the menu bar that I was overlooking?
Thanks for all of your help!
Here's the menu generating code (in AppController) that does it, should
this be helpful:
- (void)retrieveBookmarks {
Bookmark *bookmark;
NSMenu *newMenu;
NSMenuItem *newItem;
NSMutableArray *bookmarkListing;
int rowIndex;
newItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]]
initWithTitle:@"Open Bookmark" action:NULL keyEquivalent:@""];
newMenu = [[NSMenu allocWithZone:[NSMenu menuZone]]
initWithTitle:@"Open Bookmark"];
[newItem setSubmenu:newMenu];
[newMenu release];
[[NSApp mainMenu] addItem:newItem];
[newItem release];
if(!bookmarksController)
{
bookmarksController=[[BookmarkController alloc] init];
//get an NSMutableArray of Bookmark objects
bookmarkListing=[bookmarksController visibleBookmarks];
for(rowIndex=0;rowIndex<[bookmarkListing count];rowIndex++)
{
bookmark=[bookmarkListing objectAtIndex:rowIndex];
newItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]]
initWithTitle:[bookmark bookmarkTitle] action:NULL keyEquivalent:@""];
[newItem setTarget:self]; //fill this in later
[newItem setAction:@selector(noopAction)]; // fill this in later
[newMenu addItem:newItem];
[newItem release];
}
}
}
-m^2
__________
Hi! I'm a .signature virus! Copy me into your ~/.signature to help me
spread!
__________