Re: Getting Items From MainMenu
Re: Getting Items From MainMenu
- Subject: Re: Getting Items From MainMenu
- From: "Louis C. Sacha" <email@hidden>
- Date: Sun, 6 Jun 2004 18:34:55 -0700
Hello...
There are two ways you could do it, creating an outlet directly to
that menu in interface builder, or by walking the menu hierarchy.
If you add an outlet for an NSMenu to whatever controller class is
dealing with the menu, you can use that to access the menu without
having to use any extra code. The trick is managing to connect your
outlet in InterfaceBuilder.
The easiest way is to switch the window that shows the top level
objects in the nib into outline mode, and then you can open up the
hiearchy of the NSMenu (MainMenu) object to find the menu you want
and make the connection.
An example of connecting the NSMenu for the "File" menu to an outlet
in a controller:
http://www.syndrome3d.com/temp/menuConnect.gif
You can also walk the menu hierarchy to get to the menu you want.
You would get the main menu just like you do below.
Then you need to get the menu item that contains the submenu you
want, which for your example in the picture would be "My Menu
Item"(using itemWithTag: would be the best way, since itemWithTitle:
will break if your application is localized). You can set the tag
used for any menu item using the inspector in IB (including those
that are part of the main menu and appear as part of the menu bar).
Once you get the menu item for the submenu, you can use [menuItem
submenu] to get the associated menu.
For your example in the picture, you would repeat the above step to
get the NSMenu associated with the "My Sub Folder" menu item.
Hope that helps,
Louis
Hey guys,
In Interface Builder, I have a menu laid out for my MainMenu.nib. In
one menu in the MainMenu, I have another submenu. What I am wanting
to do, is programatically add items to the submenu in the other
menu item. So I first realized I needed to get the MainMenu, which
is easy:
NSMenu *mainMenu = [NSApp mainMenu];
That works fine, I then realized I needed to get the NSMenu of the
actual item inside the MainMenu. I looked inside the NSMenu docs,
but only found a way of getting NSMenuItem's (itemWithTitle:).
Right now my code is:
- (void)setupCompletionMenu
{
NSMenu *mainMenu = [NSApp mainMenu];
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"Auto
Complete" action:nil keyEquivalent:@""];
[mainMenu insertItem:item atIndex:4];
NSMenu *completionModesMenu = [[NSMenu alloc]
initWithTitle:@"Auto Complete"];
int i;
for (i = 0; i < [[docTextView availableCompletionModes] count]; i++)
{
[completionModesMenu addItemWithTitle:[[docTextView
availableCompletionModes]objectAtIndex:i]action:
@selector(changeCompletionMode:) keyEquivalent:@""];
}
[mainMenu setSubmenu:completionModesMenu forItem:item];
[completionModesMenu release];
}
And this successfully inserts a menu called Auto Complete into the
MainMenu, so basically, I want to take this code, and transfer it so
it enters the above data into another menu inside another menu.
This probably doesn't make sense, so I uploaded a picture of what
the menu looks like:
http://www.talacia.com/junk/menu.jpg
Hope someone can help,
Oliver
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.