Re: hierarchical menus
Re: hierarchical menus
- Subject: Re: hierarchical menus
- From: Matt Neuburg <email@hidden>
- Date: Tue, 14 Mar 2006 09:29:41 -0800
- Thread-topic: hierarchical menus
On Mon, 13 Mar 2006 16:36:22 -0800, Boyd Collier
<email@hidden> said:
>I'm trying to create a hierarchical menu, as follows: When the user
>selects my "Tools" menu on the menu bar, one of the menu items is
>"Scan Data." I want the submenu that "Scan Data" shows to be a list
>of open documents that the user then selects from, so this list has
>to be created on the fly. Here's what I've done thus far:
>
>- (IBAction)scanDataMenuAction:(id)sender {
>
> NSLog(@"scan data menu item was selected"); // for testing
> NSString *menuItemTitle = [(NSMenuItem*)sender title];
> NSLog(@"Here's the menu item title: %@", menuItemTitle); // this
>shows that the Scan Data item was selected.
>
> NSMenu *mySubmenu = [[NSMenu alloc] initWithTitle:@"My Submenu"]; //
>just for testing
> [(NSMenuItem*)sender setSubmenu:mySubmenu]; // I've tried lots of
>variations on this
> [mySubmenu update];
>}
>
>None of the many variations I've tried on the above get the job done.
It would be better to modify the menu each time the user opens a document.
If, however, you really must create the menu on the fly, a good place is
validateMenuItem. Example:
- (IBAction)myAction:(id)sender
{
NSLog(@"%@", [sender title]);
}
- (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem {
if ([[menuItem title] isEqualToString: @"Things"]) {
NSArray* pep =
[NSArray arrayWithObjects: @"Mannie", @"Moe", @"Jack", nil];
NSMenu* m = [[[NSMenu alloc] init] autorelease];
[m addItemWithTitle:[pep objectAtIndex: rand() % 3]
action:@selector(myAction:) keyEquivalent:@""];
[m addItemWithTitle:[pep objectAtIndex: rand() % 3]
action:@selector(myAction:) keyEquivalent:@""];
[m addItemWithTitle:[pep objectAtIndex: rand() % 3]
action:@selector(myAction:) keyEquivalent:@""];
[menuItem setSubmenu:m];
}
return YES;
}
m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden