Re: Dynamic menu building
Re: Dynamic menu building
- Subject: Re: Dynamic menu building
- From: j o a r <email@hidden>
- Date: Fri, 15 Aug 2003 18:54:26 +0200
On den 15 augusti 2003, at 18:16:11PM, April Gendill wrote:
Appearantly you did not understand what I was saying.
It was not that easy...
I CAN add single items to any menu, the dock menu, any of the main
menu bar items. I can add anything ITEM. addItem works fine even if I
am pulling data off of a file. HOWEVER to dynamically insert a whole
submenu
If you have your data structure made up of arrays and dictionaries that
look like this:
<array>
<dict>
<item>
<key> MY_TITLE_KEY</key>
<value>"a title"</value>
</item>
<item>
<key> MY_TITLE_KEY</key>
<value>"a title"</value>
<key>MY_SUBMENU_KEY</key>
<value>YES</value>
<key>MY_ITEMS_KEY</key>
<value>
<array>
// A similar structure as this one, nested
</array>
</value>
</item>
</dict>
</array>
You could do someting like this:
- (void) createMenuFromStringRepresentation:(NSString *) strRep
{
NSArray *reps = [strRep propertyList];
[self addItemRepresentations: reps toMenu: masterMenu];
}
- (void) addItemRepresentations:(NSArray *) reps toMenu:(NSMenu *) menu
{
NSEnumerator *enumerator = [reps objectEnumerator];
NSDictionary *itemRep;
while ((itemRep = [enumerator nextObject]))
{
NSMenuItem *item = [[[NSMenuItem alloc] initWithTitle: [itemRep
objectForKey: MY_TITLE_KEY] action: NULL keyEquivalent: @""]
autorelease];
BOOL isSubmenu = [itemRep objectForKey: MY_SUBMENU_KEY] != nil;
if (isSubmenu)
{
NSMenu *subMenu = [[[NSMenu alloc] initWithTitle: @""] autorelease];
// Use recursion here to traverse the tree
[self addItemRepresentations: [itemRep objectForKey: MY_ITEMS_KEY]
toMenu: subMenu];
[item setSubmenu: subMenu];
}
[menu addItem: item];
}
}
Note! I just hacked this together in Mail, so it probably doesn't work
at all - but it should give you some idea as to how you can make things
work.
this will iterate through the array or dictionary or what ever, it
will assign the items to the submenu aMenu, but if I nest that loop in
a loop that moves through an array of arrays or dictionaries won't all
of the items added to aMenu be wiped on the next dictionary that gets
stepped through on that while loop OR, will it be ok because the aMenu
is re initialized each time it steps a new dictionary, and it's re
initialized with a new name?
It will work fine.
I am really trying to understand if cocoa is going to get confused if
I pass the same variable name for the menu a hundred times with
different initWithTitle: information?
You need to understand what the _scope_ is for a variable name. For a
variable that you declare in a method it's limited to the current
block. Pick up your favourite tutorial / book on C and learn more about
this!
j o a r
_______________________________________________
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.