使用 View 和 FirstResponder 验证 NSToolbarItem
我有一个包含视图(NSButton
)的工具栏项目,并且该视图的目标设置为nil
,因此它会在触发时查找响应者链行动。不过,我在尝试如何针对第一响应者进行验证时遇到了麻烦。
我对 NSToolbarItem 进行了子类化并实现了 -(void)validate
。我找到的参考文献说,您应该能够通过调用 [[[self view] window]firstResponder]
来获取第一个响应者,然后查看响应者链是否实现 NSToolbarItemValidation
或 NSUserInterfaceValidations
并调用适当的验证方法。
但是,当我调用 [[self view] window]
时,我将返回 nil
作为窗口,因此我无法检索第一个响应者。
我不确定这是否有什么不同,但我在 XIB 中创建工具栏,而不是用代码编写它。
查看 NSToolbar.h,有一个 ivar 引用包含窗口,因此我可以通过调用 [[[selftoolbar] valueForKey:@"window"]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 toolbar] valueForKey:@"window"] firstResponder]
, but that's a bit of a hack and I'd prefer to use something documented/stabler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不手动验证。应用程序对响应者链中的每个对象调用 -validate (或其适当的变体),对于需要验证的每个菜单项或工具栏项,直到找到返回 YES 的对象(意味着它支持每个菜单或工具栏项的操作) ),或者没有找到。如果找到,则您的工具栏项或菜单项已启用。如果没有,则被禁用。至少,这是一般理论。
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.