使用 View 和 FirstResponder 验证 NSToolbarItem

发布于 2024-09-10 09:02:29 字数 662 浏览 2 评论 0原文

我有一个包含视图(NSButton)的工具栏项目,并且该视图的目标设置为nil,因此它会在触发时查找响应者链行动。不过,我在尝试如何针对第一响应者进行验证时遇到了麻烦。

我对 NSToolbarItem 进行了子类化并实现了 -(void)validate。我找到的参考文献说,您应该能够通过调用 [[[self view] window]firstResponder] 来获取第一个响应者,然后查看响应者链是否实现 NSToolbarItemValidationNSUserInterfaceValidations 并调用适当的验证方法。

但是,当我调用 [[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2024-09-17 09:02:29

您不手动验证。应用程序对响应者链中的每个对象调用 -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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文