• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Dynamic menu building
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Dynamic menu building


  • Subject: Re: Dynamic menu building
  • From: April Gendill <email@hidden>
  • Date: Fri, 15 Aug 2003 11:12:33 -0700

On Friday, August 15, 2003, at 09:54 AM, j o a r wrote:

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>

I don't read XML very well, but what I understand says this is about what I'm doing. there is some extra info in the dictionaries but basically for this part of the process the name of the item and a value for it being a submenu is all that gets read.


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.

I don't need it to work I'm not going to cut and paste it into the app anyway.. The only thing you learn from cutting and pasting is how to cut and paste.
And although I could not articulate my question very well, this part answers it I think
Tell me if I understand what I see:
I see here that I can gather the data globally or locally either way as long as I have the correct data and know the keys, I can begin a loop based on what ever identifier I need to use in order to get a loop going.
Inside the loop I extract a submenu, initialize it with a static variable, give it a name based on the data, add any items to it that need adding, then stick it on the master menu. Next time through the loop I start with the next dictionary of items, and it does not matter that the variable name is the same because the loop will not overwrite it's previous itteration, it will simply add the new information.

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!

again, it is scope that was confusing me since the variables in question are within the same method.




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.
_______________________________________________
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.

  • Follow-Ups:
    • Re: Dynamic menu building
      • From: j o a r <email@hidden>
References: 
 >Re: Dynamic menu building (From: j o a r <email@hidden>)

  • Prev by Date: Re: Dynamic menu building
  • Next by Date: Re: help trying to locate sigsegv error please
  • Previous by thread: Re: Dynamic menu building
  • Next by thread: Re: Dynamic menu building
  • Index(es):
    • Date
    • Thread