views:

23

answers:

1

I've got a toolbar item that contains a view (an NSButton) and the view's target is set to nil, so it'll look up the responder chain when triggering the action. I'm having trouble trying how to validate against the first responder, though.

I've subclassed NSToolbarItem and implemented -(void)validate. The references that I've found say that you should be able to get the first responder by calling [[[self view] window] firstResponder], then seeing if the responder chain implements either NSToolbarItemValidation or NSUserInterfaceValidations and calling the appropriate validation method.

However, when I call [[self view] window], I'm getting back nil as the window, so I'm not able to retrieve the first responder.

I'm not sure if it makes a difference, but I'm creating the toolbar in the XIB instead of writing it in code.

Looking in NSToolbar.h, there's a ivar that references the containing window, so I could find the first responder by calling [[[self toolbar] valueForKey:@"window"] firstResponder], but that's a bit of a hack and I'd prefer to use something documented/stabler.

A: 

You don't validate manually. The application calls -validate (or its appropriate variant) on each object in the responder chain, for each menu item or toolbar item that requires validation, until it finds one that returns YES (meaning that it supports the action of each menu or toolbar item), or doesn't find one. If it finds one, your toolbar item or menu item is enabled. If not, it is disabled. At least, that's the general theory.

Bored Astronaut
For NSToolbarItem instances that contains views, you need to subclass and provide an implementation of -validate. I'm trying to figure out how to write that method when the target is the first responder (ie, `nil`).http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Toolbars/Tasks/ValidatingTBItems.html#//apple_ref/doc/uid/20000753-BAJGFHDD
Jablair