Getting NSPopUpButton from NSMenuItem
My application uses the -validateMenuI开发者_开发问答tem:
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?
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.
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;
}
}
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.
精彩评论