• 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
Enabling and Disabling menus
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Enabling and Disabling menus


  • Subject: Enabling and Disabling menus
  • From: email@hidden
  • Date: Wed, 22 Aug 2001 14:46:22 -0400

Hello,
I like to know how I can enable and disable menus (actually Menu Items) in my application based on the state of my application. I read the docs in Developer/Documentation/Cocoa/TasksAndConcepts/ProgrammingTopics/AppMenu.
Specifically I read the documentation on NSMenu and NSMenuValidation. But I am still confused, below is the code I added to my controller object.

- (void) awakeFromNib
{
NSMenu *mainMenu;
NSArray *itemArray;
int i;

[some code here ...............]

mainMenu = [NSApp mainMenu];
NSLog(@"menu title is %s and number of items is %d",[mainMenu title], [mainMenu numberOfItems]);
[mainMenu setAutoenablesItems:YES];

itemArray = [mainMenu itemArray];
for (i=0; i< [itemArray count]; i++) {
[[itemArray objectAtIndex:i] setTarget:self];
}

}

I am sending the message setAutoenableItems to the menu's in the above code.

Also I implemented the below method in the same class

- (BOOL) validateMenuItem: (NSMenuItem *)anItem {
if ([[anItem title] isEqualToString: @"New"])
return YES;
else
return NO;
}

This doesn't seem to have any effect at all on the application menu items.
Also I like to know what the mainMenu message to the NSApp fetch. Does the object I get from this message refer to the whole Menu Bar with all the Menu's or something else. Logically its much easier to think in terms of Menu Bar, Menus and Menu Items.

Thanks.
Sarat

PS: Is there a better way to tie the state of other controls in the program like buttons to the state of menu items. That way I can set the state (enabled/disabled) of one control and all the other controls, menu items that are related reflect the same state.


  • Follow-Ups:
    • Re: Enabling and Disabling menus
      • From: Ondra Cada <email@hidden>
  • Prev by Date: Getting the Command key modifier for a NSView
  • Next by Date: bug in TextFinder.m (also in TextEdit)
  • Previous by thread: Re: Getting the Command key modifier for a NSView
  • Next by thread: Re: Enabling and Disabling menus
  • Index(es):
    • Date
    • Thread