user1040049
user1040049

Reputation:

How to validate menu items dynamically populated via Cocoa Bindings

The Context
I'm populating a TableView's NSPopupMenuCell's menu via Cocoa Bindings. All is well (I've bound 'Content', 'Content Values' and 'Selected Objects'). The menu is correctly populated at runtime and the underlying variable is properly being set (via KVC). Everything works on that end.

My Problem
Next I'd like to control the state of the populated menu items (as some would need to be disabled). Now I've read Apple's documentation on Enabling Menu Items very carefully… but for the life of me I can't seem to implement validateMenuItem: nor validateUserInterfaceItem: where they would actually get called.

I've tried, amongst other things, implementing these methods in the already existing App delegate but no joy. Am I perhaps misunderstanding something with the First Responder chain?

Also, logically, the other possibility is that the menu items actually do have Targets (the above assumes they don't). But, again, I can't seem to be able to identify their targets (so that I may implement validateMenuItem: there).

Upvotes: 2

Views: 924

Answers (2)

user1040049
user1040049

Reputation:

The Target of dynamically (via bindings) created NSMenuItem in a NSPopupMenuCell, turns out to be the NSPopupMenuCell itself (so you can subclass it and implement validateMenuItem: there).

I would imagine it's because NSPopupMenuCell would need to perform KVC calls (i.e. setValue:forKey:).

Upvotes: 1

user23743
user23743

Reputation:

If you're using bindings to prepare the contents, you can also bind each NSMenuItem's enabled property to a boolean in the model expressing whether it should be enabled.

Upvotes: 0

Related Questions