Re: activating Delete menu item through binding
Re: activating Delete menu item through binding
- Subject: Re: activating Delete menu item through binding
- From: Koen van der Drift <email@hidden>
- Date: Sun, 15 Apr 2012 14:53:11 -0400
Excellent explanation, thanks Quincey. Interestingly, the 'Select All' menu item that sits just under the Delete item (and both of them were already present when I started my project) works 'out of the box' for my NSTableView, I did not set any responders or do any validation, etc.
- Koen.
On Apr 15, 2012, at 2:43 PM, Quincey Morris wrote:
> On Apr 15, 2012, at 11:13 , Koen van der Drift wrote:
>
>> My MainMenu.nib has a 'Delete' menu item that is mapped to the Delete key. I'd like to use that to remove items from my NSTableView that is connected to an NSArrayController. So in the bindings for the Delete menu item, I set it's availability to myArrayController.canRemove. However, the Delete menu item remains grayed out.
>>
>> Is there anything else I need to do to activate the Delete menu item?
>
> Aside from the fact that controlling the menu state this way won't work, it's not a valid approach. Even if the only things *your* code can delete are table view items, the Delete menu item needs to be available for editing text fields in any dialogs that pop up.
>
> Anyway, the reason it doesn't work is that the sequence of events is as follows, when the containing menu is about to be displayed:
>
> 1. The target of the Delete menu item is determined. The outcome of this step depends on whether it's configured with a specific target or its target is nil (i.e. First Responder).
>
> 1a. If there's an explicit target, the target must implement the action method, otherwise the menu item is disabled.
>
> 1b. If there target is nil, the responder chain is traversed looking for a responder that implements the action method. If none is found, the menu item is disabled.
>
> 2. If the target implements user interface item validation (or one of its variants, such as menu item validation), validation must return YES. Note that validation might pass up the class hierarchy via a 'super' invocation, or be delegated to other objects, depending on the implementation in the target object.
>
> If the validation returns YES, or if the target doesn't implement validation, then the menu item is enabled, otherwise disabled.
>
> So, what responder object do you have that implements the 'delete:' action? What object in the responder chain implements user item validation? Note that NSArrayController is not a responder and is not in the responder chain. In a document-based app, a window controller or document would likely be responsible for this sort of thing. In a non-document app, the app delegate or the main window controller would usually do it.
>
> Note that if you have a delete *button* in your window, you can target its action directly to the array controller, in which case the binding can be used for the button enable state. Menus (being shared app-wide) are different.
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden