Validating NSToolbarItem with View against FirstResponder
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 toolba开发者_JAVA技巧r] valueForKey:@"window"] firstResponder]
, but that's a bit of a hack and I'd prefer to use something documented/stabler.
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.
精彩评论