Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Menu Item Validation

Menu Item Validation
Thread Tools
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Apr 6, 2003, 04:36 PM
 
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
Reply With Quote
Apr 6, 2003, 05:25 PM
 
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
     
Brass  (op)
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Apr 6, 2003, 05:34 PM
 
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
Reply With Quote
Apr 7, 2003, 06:12 AM
 
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.
Rick Roe
icons.cx | weblog
     
Brass  (op)
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Apr 7, 2003, 05:37 PM
 
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
Reply With Quote
Apr 7, 2003, 11:50 PM
 
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.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:21 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2