• 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: hierarchical menus (SOLVED)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: hierarchical menus (SOLVED)


  • Subject: Re: hierarchical menus (SOLVED)
  • From: Boyd Collier <email@hidden>
  • Date: Tue, 14 Mar 2006 11:07:23 -0800

Matt,

Thanks very much! Your suggestion was a great help and saved me many hours.

Re Apple's documentation: curiously, validateMenuItem isn't mentioned, AFAIK, in the documentation of either NSMenuItem or NSMenu. Rather, it's in NSDocument and not adequately explained there.

Thanks again, Boyd


On Mar 14, 2006, at 9:29 AM, Matt Neuburg wrote:

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
  • Follow-Ups:
    • Re: hierarchical menus (SOLVED)
      • From: Matt Neuburg <email@hidden>
References: 
 >Re: hierarchical menus (From: Matt Neuburg <email@hidden>)

  • Prev by Date: Re: Getting a bullet into a NSString
  • Next by Date: Re: Getting a bullet into a NSString
  • Previous by thread: Re: hierarchical menus
  • Next by thread: Re: hierarchical menus (SOLVED)
  • Index(es):
    • Date
    • Thread