Re: FW: Enabling and Disabling menus
Re: FW: Enabling and Disabling menus
- Subject: Re: FW: Enabling and Disabling menus
- From: Henri Lamiraux <email@hidden>
- Date: Wed, 22 Aug 2001 12:59:56 -0700
Sorry but setting and unsetting the action of a menu item to enable and
disable it is not the right way to do it.
First just connect your menu items in IB to the appropriate action of
the appropriate target object (don't do it by code unless you have a
good reason).
Lets say that you have connected the menu item titled "Do Something" to
the -doSomething: method of your target object (either an object
instantiated in your nib or the File's Owner or First Responder proxy
objects)
To enable or disable this item depending on some state of your app
simply implements the -validateMenuItem: method in the target object
@implementation
<WhatEverTheClassOfTheTargetObjectConnectedToThisMenuItem>
....
- (void)doSomething:(id)sender // the action method trickered by the
menu item
{
......
}
....
- (BOOL)validateMenuItem:(NSMenuItem*)menuItem
{
SEL action = [menuItem action];
if (action == @selector(doSomething:)) {
return <YES or NO depending on the state of your app>
} else if (action == ........)
....
} .....
return [menuItem isEnabled];
}
....
@end
Do not test the menu item title. This completely breaks localization.
On Wednesday, August 22, 2001, at 12:07 PM, Stuppel, Searle @ San Diego
Central wrote:
this is not the way to do it... let me share with you the code that
actually
WORKS
//// the .h:
#import <Cocoa/Cocoa.h>
@interface MyMenu : NSMenu
{
IBOutlet id menuClear;
IBOutlet id menuGenerate;
IBOutlet id menuSaveHTML;
IBOutlet id menuSaveText;
SEL actClear, actGenerate, actSaveHTML, actSaveText;
}
- (void)setMenu:(id)sender mG:(BOOL)gen mC:(BOOL)cle mST:(BOOL)mst
mSH:(BOOL)msh;
@end
//// the .m
#import "MyMenu.h"
#import "Controller.h"
@implementation MyMenu
-(void)awakeFromNib
{
actGenerate = [menuGenerate action];
actClear = [menuClear action];
actSaveText = [menuSaveText action];
actSaveHTML = [menuSaveHTML action];
}
- (void)setMenu:(id)sender mG:(BOOL)gen mC:(BOOL)cle mST:(BOOL)mst
mSH:(BOOL)msh
{
if (gen) {
[menuGenerate setAction:actGenerate];
}
else {
[menuGenerate setAction:0];
}
if (cle) {
[menuClear setAction:actClear];
}
else {
[menuClear setAction:0];
}
if (mst) {
[menuSaveText setAction:actSaveText];
}
else {
[menuSaveText setAction:0];
}
if (msh) {
[menuSaveHTML setAction:actSaveHTML];
}
else {
[menuSaveHTML setAction:0];
}
}
@end
/// the actual call from another file:
[menuLink setMenu:self mG:YES mC:NO mST:NO mSH:NO];
--- where menuLink is an outlet link from the calling file to the menu
file.
--- also you need to import the menu files .h file if you land up doing
this
in separate files
let me know if this helps
searle
Searle Stuppel
CB Richard Ellis, Inc.
Direct: 858-546-4600
Fax: 858-546-4616
Toll Free: 800-334-9347
email@hidden
-----Original Message-----
From: email@hidden
[mailto:email@hidden]On Behalf Of email@hidden
Sent: Wednesday, August 22, 2001 11:46 AM
To: email@hidden
Subject: Enabling and Disabling menus
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.
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
_______________________________________
Henri Lamiraux
Engineering Manager
User Interface Tools Group
Apple
_______________________________________