Re: Dynamic menu building
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 with
Data:[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
with
Data:[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.