views:

282

answers:

2

Hi,

My application uses the -validateMenuItem: method for validating menu items. But I need to validate different menu items depending on what popup's there in.

I was hoping for a way to get the tag of the popup, but after looking through the docs I can't seem to find a way... any ideas?

Thanks, Marcus

Edit: I thought this needed some more context... my model object is a JDBCSyncer (syncs one database with another), my window is a settings one, and I need to validate my menu items based on wether their title is in an array of strings which represents the various fields within a table. The idea is that you select the field from a popup

+1  A: 

I don't have a great answer off the top of my head, but how about something along these lines:

- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
    NSMenu *menu = [menuItem menu];
    if (menu == [popUpButton1 menu]) {
        return YES;
    }
    else if (menu == [popUpButton2 menu]) {
        return NO;
    }
    else (menu == [popUpButton3 menu]) {
        return YES;
    }
    else {
        return NO;
    }
}
Carl Norum
That won't work with submenus.
Peter Hosey
+1  A: 

If the menu items in these pop-up buttons are so unrelated that you need to distinguish one pop-up button from the other, perhaps you should create separate controller objects for them. Each controller would be the target (and, thus, the validator) of its pop-up button(s)' menu items, and only that/those pop-up button(s).

Some or all of these controllers can also feed table views or collection views, if that makes sense for the items in question.

This also lets you tag the menu items for easy identification, without worrying about tag collisions (same tag used in two or more unrelated UI elements), since each controller will only see the tags it knows about. Similarly, if a controller uses represented objects (most probable if it dynamically populates its pop-up button(s)), it doesn't have to worry about seeing represented objects it doesn't recognize.

Peter Hosey
This seems to be a good idea - I have created a new set of controllers and they are receiving validateMenuItem: as planned. But now I have another problem, I was validating the NSMenuItem by checking it's title was in an NSArray. This array was returned from my model object - which is owned by my 'main' controller.So now my new controllers don't have access to this array.I'm sure this is something very simple but i'm fairly new to cocoa and still trying to grasp the design pattern,Cheers,Marcus
Marcus Wood
And also, my popups contents had bindings to that NSArray
Marcus Wood
So your model object owns an array of titles? That sounds like part of a parallel array, which is wrong. You should have an array of model objects, each of which has one title. Remember, a model object models *something*; in this case, it's something that has a title.
Peter Hosey
Maybe I should give it some more context:My model object is a JDBCSyncer (syncs one database with another), my window is a settings one, and my array is an array of strings representing the various fields within a table. The idea is that you select the field from a popup.I can't really have multiple JDBCSyncer objects as there is only ever two databases I need to keep in sync... is this in itself a bad implementation?Thanks for your time :),Marcus
Marcus Wood
Marcus Wood: That sounds more like a controller. A model object would represent the table or a single column.
Peter Hosey
Hehe.. But I will still need my JDBCSyncer to grab the list of columns and this can only be owned by one controller right?
Marcus Wood
The controller would grab the list of columns for the table when creating the model object that represents that table and (if you decide to do this) the model objects that represent those columns.
Peter Hosey
OK, but when I grab the list of columns to stick in a Table object I can only do that from one controller (the one which owns the JDBCSyncer). So I can't then get a different Table object per controller - which would be necessary to validate the menu items on the columns within said Table object. Does this make sense? Thanks, Marcus
Marcus Wood
I thought it was the JDBCSyncer that grabs the list of columns.
Peter Hosey
Yes thats right, but I can only have one JDBCSyncer to grab columns in one controller...
Marcus Wood
I don't know why any other controllers are relevant. The syncer is the only controller that matters here. It should create a model object representing a table, which should own model objects representing columns; if there may be multiple tables to be synced, then the syncer should create and own multiple table objects.
Peter Hosey
I thought multiple controllers are necessary because of the original problem with -validateMenuItem: and I can't understand how that setup will help. Sorry about this, maybe I'm getting confused but I really appreciate the help
Marcus Wood