macoscocoansruleeditor

Performing action when NSRuleEditor criterion is selected


How can I determine which criterion is being selected in the NSPopUpButton menu of an NSRuleEditor? Inside ruleEditorRowsDidChange(_:)

I could track the criteria for each row and process the changes manually, but it seems like there should be an easier way.

I tried returning an NSMenuItem instead of a String for the criterion's value in the rule editor's delegate; then setting the target and action properties on the menu item. That allows me to have a method called when the menu item (criterion) is selected in the rule editor. It also means I know which criterion triggered the action method because each menu item's target is the criterion itself. However, setting the target and action properties on the menu item overrides the private target and action values that the rule editor sets on the menu item. This means that after selecting a criterion in the rule editor, the target/action that I set fires, but the selection change is not reflected in the rule editor.

My current solution is to return an NSPopUpButton as the criterion's displayValue (since it inherits from NSView) and manage the button's items and target/action manually. This works well but feels like it breaks the design of the rule editor a bit by having a single criterion managing multiple values itself instead of relying on the rule editor's delegate).

Am I going about this the wrong way? Is there a simpler method to run criterion-specific code when a criterion is selected within a rule editor?

Thanks!


Solution

  • Set the action of the menu item and inside the action call - (void)setCriteria:(NSArray *)criteria andDisplayValues:(NSArray *)values forRowAtIndex:(NSInteger)rowIndex of the rule editor to refresh the row. You can get the row with - (NSInteger)rowForDisplayValue:(id)displayValue.