Re: PopupButton help
Re: PopupButton help
- Subject: Re: PopupButton help
- From: Lorenzo <email@hidden>
- Date: Tue, 09 Dec 2003 19:00:25 +0100
Hi,
you could populate the menu as described below.
You should earlier store a NSMutableArray containing the titles of the menu
items. If the title is empty @"", I add a menu separator.
Just call this method and your popMenu will be synchronized with the mArray.
- (void)populatePopMenu
{
int i;
NSString *itemTitle;
[popMenu removeAllItems];
for(i = 0; i < [mPopArray count]; i++){
itemTitle = [mPopArray objectAtIndex:i];
if([itemTitle length] > 0){
[popMenu addItemWithTitle:itemTitle];
[[popMenu itemAtIndex:i] setTag:i];
}
else{
[[popMenu menu] addItem:[NSMenuItem separatorItem]];
}
}
}
The tag of each item here above are just put sequentially. But if you need
to join item titles and fixed item tags (to recall the methods by the tag
later), you should store, as single object of the mArray, a NSDictionary
containing 2 keys: itemTitle and itemTag:
-------
NSMutableDictionary *itemDict = [NSMutableDictionary dictionary];
[itemDict setObject:@"myMenuTitle" forKey:@"itemTitle"];
[itemDict setObject:[NSNumber numberWithInt:4] forKey:@"itemTag"];
[mArray addObject:itemDict ];
-------
Then populate the popMenu this way:
- (void)populatePopMenu
{
int i, itemTag;
NSDictionary *itemDict;
NSString *itemTitle;
[popMenu removeAllItems];
for(i = 0; i < [mPopArray count]; i++){
itemDict = [mPopArray objectAtIndex:i];
itemTitle = [itemDict objectForKey:@"itemTitle"];
itemTag = [[itemDict objectForKey:@"itemTag"] intValue];
if([itemTitle length] > 0){
[popMenu addItemWithTitle:itemTitle];
[[popMenu itemAtIndex:i] setTag:itemTag];
}
else{
[[popMenu menu] addItem:[NSMenuItem separatorItem]];
}
}
}
Anyway, I don't rememebr well now, but I suppose that you can store, within
the mPopArray, the menuItem objects directly... so you don't have to build
keys, dictionaries...
Best Regards
--
Lorenzo
email: email@hidden
>
Message: 14
>
To: email@hidden
>
From: Kurt Marek <email@hidden>
>
Subject: PopupButton help
>
Date: Tue, 9 Dec 2003 08:42:18 -0800
>
>
I'm just getting started trying to implement an NSPopupButton and I'm
>
having trouble figuring out the easiest way to populate the NSMenu that
>
goes along with it. The popupButton will be used in conjunction with an
>
'add' button so that the user can quickly add attributes from the
>
popupbutton that correspond to their document, like categories such as
>
Friends, Family, Coworkers, etc. I want the popupbutton's NSMenu to be
>
customizable, so I need an 'Edit list' menuitem always present.
>
>
Here are my questions:
>
>
1) Is there a way to bind the contents of an NSMenu to an array? Then I
>
could just add/remove objects from the array during the 'Edit list'
>
method.
>
>
2) How do I put in a divider?
>
>
Thanks,
>
Kurt
_______________________________________________
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.