• 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: Shawn Erickson <email@hidden>
  • Date: Sat, 16 Aug 2003 17:13:38 -0700

On Friday, August 15, 2003, at 12:42 AM, April Gendill wrote:

Ok...
Dynamic menu building is as far as I can tell very complex.
1 you have to create some form of data structure
2 you have to build the menu on the fly based on said(ever changing)
data.

[snip]

What I would like to do is create a program that can gather data from
a file and create a menu, with submenus displaying items from a
dictionary. Each dictionary would be a submenu all it's own and since I
cannot know the names and number of submenus that might be added, how
do I allocate the submenu item so it can be added to the toplevel.

For the archives...

This question was asked again on another using the address Laze E Development <email@hidden>. The following is a quick example of how one can do this and my answer doesn't much vary from what others have already said.

(download project & source: http://207.71.250.113/~shawnce/MenuExample.sit)

#import <Cocoa/Cocoa.h>

@interface DynamicMenuController : NSObject
{
IBOutlet NSMenu *dynamicMenu;
}
- (IBAction)selectionMade:(id)sender;
- (NSMenuItem*) menuItemWithName:(NSString*)name;
- (void)populateMenu:(NSMenu*)menu withData:(NSArray*)data;
- (NSArray*)loadMenuData;
@end

@implementation DynamicMenuController

- (void)awakeFromNib
{
[self populateMenu:dynamicMenu withData:[self loadMenuData]];
}

- (IBAction)selectionMade:(id)sender
{
NSLog(@"Selection made: %@", sender);
}

- (NSArray*)loadMenuData
{
int i;

NSMutableArray* menuData = [NSMutableArray array];
[menuData addObject:@"First Item"];
[menuData addObject:@"Second Item"];

for (i = 0; i < 3; i++) {
int j;
NSMutableArray* submenu = [NSMutableArray array];
[submenu addObject:[NSString stringWithFormat:@"SubMenu%d", i]];

for (j = 0; j < 5; j++) {
[submenu addObject:[NSString stringWithFormat:@"SubMenu%dItem%d", i, j]];
}

[menuData addObject:submenu];
}

[menuData addObject:@"Last Item"];

NSLog(@"Fake Menu Data: %@", menuData);

return menuData;
}

- (void)populateMenu:(NSMenu*)menu withData:(NSArray*)data
{
NSEnumerator* enumerator = [data objectEnumerator];
id anObject;

while (anObject = [enumerator nextObject]) {
if ([anObject isKindOfClass:[NSString class]]) {
[menu addItem:[self menuItemWithName:anObject]];
} else {
NSMenuItem* submenuItem;

NSMenu* submenu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
[submenu setAutoenablesItems:NO];

submenuItem = [self menuItemWithName:[anObject objectAtIndex:0]];
[menu addItem:submenuItem];
[menu setSubmenu:submenu forItem:submenuItem];

[self populateMenu:submenu
withData:[anObject subarrayWithRange:NSMakeRange(1,[anObject count]-1)]];
}
}
}

- (NSMenuItem*) menuItemWithName:(NSString*)name
{
NSMenuItem* item = [[[NSMenuItem alloc] init] autorelease];

[item setTitle:name];
[item setEnabled:YES];
[item setTarget:self];
[item setAction:@selector(selectionMade:)];

return item;
}

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

References: 
 >Dynamic menu building (From: April Gendill <email@hidden>)

  • Prev by Date: Re: Override a Finder command
  • Next by Date: Re: Authorized operations
  • Previous by thread: Re: Dynamic menu building
  • Next by thread: Recent Items
  • Index(es):
    • Date
    • Thread