Challenge 2, Chap. 21


I successfully added a menu item on the main menu that does remove selected employees as the challenge asks. However, unlike the button on the actual document, it does not become inactive when no lines are selected on the table. I could not find a way to bind any Main Menu item to the arrayController nor could I find any clean method to directly influence it. Would the correct answer be an observer in the AppController (or somewhere else?) that sends a message to a method which controls if the menu item is enabled or not? Or is there a more direct method I am just not seeing?


I didn’t think of this until I saw your post. You need to implement validateMenuItem: in RMDocument.m. I’ll post the solution if you want, but for now you can just look it up in Apple’s documentation.


I looked up the Apple documentation:[quote]validateMenuItem:
Validates the Revert menu item and items selected from the Save panel’s pop-up list of writable document types items. (Available in Mac OS X v10.0 through Mac OS X v10.3. Use validateUserInterfaceItem: instead.)[/quote]
So I have used the recommended method and here is the code:

[code]- (BOOL)validateUserInterfaceItem:(id )anItem
SEL action = [anItem action];

if ((action == @selector(removeEmployee:)) & ([tableView selectedRow] == -1)) {
    // Disable menu item
    return NO;
return YES;

Thanks John for the advice!



Great tip!

What confuses me though is how does the validateMenuItem: method relate to our specific menu item for removing employees, and that specific menu item only?