 |
 |
Menu Item Validation
|
 |
|
 |
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
What's the best way to validate menu items for the purpose of making each item either available (black and active) or unavailable (grey and inactive)?
I've been using the delegate method, "validateMenuItem" identifying each menu item by it's item title. This works fine for me.
However, it recently occured to me that surely this wouldn't be working for localised versionf of the app, because of course, each menu item has a different title in each different locale.
So I thought that I could assign each item with a tag, but then I thought that might end up being a maintenance nightmare too.
Is this what other people too? Assign tags to menus which should be inaccessible sometimes, and use those tags for validation?
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Dec 2001
Status:
Offline
|
|
I use one of two techniques, neither of which is ideal, but then nothing in C is ever quite ideal:
1) Use tags, and create a header file with an enum statement which defines the english names for the menu items as the corresponding tag numbers. It's optimization, localization, and documentation all in one!
2) If you only have a few menu items in need of validation, just create IBOutlets for them. That way you can compare them using == instead of isEqual:, which requires (at least one) additional objc_msgSend(), plus you still get to compare them by name in your code.
|
|
"Think Different. Like The Rest Of Us."
iBook G4/1.2GHz | 1.25GB | 60GB | Mac OS X 10.4.2
Athlon XP 2500+/1.83GHz | 1GB PC3200 | 120GB | Windows XP
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
Originally posted by macmike42:
I use one of two techniques, neither of which is ideal, but then nothing in C is ever quite ideal:
1) Use tags, and create a header file with an enum statement which defines the english names for the menu items as the corresponding tag numbers. It's optimization, localization, and documentation all in one!
2) If you only have a few menu items in need of validation, just create IBOutlets for them. That way you can compare them using == instead of isEqual:, which requires (at least one) additional objc_msgSend(), plus you still get to compare them by name in your code.
Thanks for these two good ideas. I will have to think about which suits my needs best, but they both sound workable.
It's a shame that we have to think about such things with Cocoa and Objective-C. You'd think we could have an NSString tag rather than an integer tag, or something like that, to avoid that hassle we seem to have to go through for simple item validation.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status:
Offline
|
|
All our apps validate menu and toolbar items based on the item's action selector:
Code:
- (BOOL)validateMenuItem(id <NSMenuItem> anItem);
{
SEL action = [item action];
if (action == @selector(doThis))
return [self canDoThis];
else if (action == @selector(doThat))
return [self canDoThat];
else
return NO;
}
Works pretty well for being localization-independent. The only time it's been a problem is when validation changes the item title and we're using the same selector for two different items (in the main menu and a contextual menu) which we want to have different titles. But that's a pretty rare problem that's easy to work around, and otherwise this scheme works pretty well -- don't have to worry about localizers messing up the tags or outlets in a nib, either.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status:
Offline
|
|
Rick,
That is an excellent solution to the problem! I love it!
Thank you so much for your suggestion.
Now I've got to go and re-write all my menu validation code...
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
I hate to jump in late just to say "Me Too," but I use the selector method as well. I originally got it from somewhere on Apple's site, so it is documented somewhere.
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|