NSMenu not displaying items added programmatically
NSMenu not displaying items added programmatically
- Subject: NSMenu not displaying items added programmatically
- From: Patrick Hartling <email@hidden>
- Date: Thu, 10 May 2007 09:00:29 -0500
- Openpgp: id=C874F217; url=http://keyserver.veridis.com:11371/export?id=-4495319854773636585&created=1069733607000
I am running into a problem with NSMenu where menu items that I add throu=
gh
-insertItemWithTitle:action:keyEquivalent:atIndex: are never displayed. W=
hat
I am trying to do is manual management of recently opened files. The file=
s
being opened are not technically documents; they are configuration files =
for
the software. From reading about NSDocumentController and NSDocument, it
seemed that these were not appropriate for my case.
There are two places in my code where I add items to the "Open Recent" me=
nu.
The first is in my NSApplication delegate's initializer. In this case, it=
opens a plist file containing a dictionary which in turn contains an arra=
y
of strings that are the recently opened files. The code for this is rough=
ly
the following:
NSMenu* file_menu =3D nil;
NSApplication* app =3D [NSApplication sharedApplication];
NSMenuItem* item =3D [[app mainMenu] itemWithTitle:@"File"];
if ( item )
{
item =3D [[item submenu] itemWithTitle:@"Open Recent"];
if ( item )
{
file_menu =3D [item submenu];
}
}
if ( file_menu )
{
const unsigned int count =3D [mRecentCfgFiles count];
[file_menu setMenuChangedMessagesEnabled:NO];
for ( unsigned int i =3D 0; i < count; ++i )
{
NSString* title =3D [mRecentCfgFiles objectAtIndex:i];
NSString* accel =3D [NSString stringWithFormat:@"%d", i];
NSMenuItem* file_item =3D
[file_menu insertItemWithTitle:title
action:@selector(loadConfigFile:)
keyEquivalent:accel
atIndex:i];
[file_item setKeyEquivalentModifierMask:NSCommandKeyMask];
NSLog(@"Inserted item %@ at index %d in menu %@\n",
file_item, i, file_menu);
}
[file_menu setMenuChangedMessagesEnabled:YES];
[file_menu update];
}
(Sending file_menu -setMenuChangedMessagesEnabled: and -update are new
additions from this morning that I thought for sure would fix the bug, bu=
t
they make no difference.)
When the above code is executed, I get output such as this:
2007-05-10 07:50:22.237 cubes[14070] Inserted item <MenuItem: 0x5c333a0
/Users/patrick/sim.wand.mixin.jconf> at index 0 in menu <NSMenu: 0x5c3552=
0>
Title: Open Recent
Supermenu: 0x5c352f0 (File), autoenable: YES, change messages
enabled: NO
Items: (
<MenuItem: 0x5c333a0 /Users/patrick/sim.wand.mixin.jconf>,
<MenuItem: 0x5c360e0 >,
<MenuItem: 0x5c35a20 Clear Menu>
)
2007-05-10 07:50:22.238 cubes[14070] Inserted item <MenuItem: 0x5c328f0
/Users/patrick/sim.base.jconf> at index 1 in menu <NSMenu: 0x5c35520>
Title: Open Recent
Supermenu: 0x5c352f0 (File), autoenable: YES, change messages
enabled: NO
Items: (
<MenuItem: 0x5c333a0 /Users/patrick/sim.wand.mixin.jconf>,
<MenuItem: 0x5c328f0 /Users/patrick/sim.base.jconf>,
<MenuItem: 0x5c360e0 >,
<MenuItem: 0x5c35a20 Clear Menu>
)
This is the structure that I expect, but when I open the submenu, all it =
has
is the "Clear Menu" item.
The second place that I modify the "Open Recent" menu is in response to t=
he
user activating the "Open Configuration..." menu item. In that case, the
code above is the same except that there is no loop involved since only o=
ne
item is being added to the menu.
Is there something that I need to do to tell the menu to update its displ=
ay,
or am I just misusing NSMenu?
-Patrick
--=20
Patrick L. Hartling
VP Engineering, Infiscape Corp.
http://www.infiscape.com/
Attachment:
signature.asc
Description: OpenPGP digital signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden