简体   繁体   English

针对FirstResponder使用视图验证NSToolbarItem

[英]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. 我有一个工具栏项目,其中包含一个视图(一个NSButton ),并且该视图的目标设置为nil ,因此在触发操作时它将查找响应者链。 I'm having trouble trying how to validate against the first responder, though. 不过,我在尝试如何针对第一响应者进行验证时遇到了麻烦。

I've subclassed NSToolbarItem and implemented -(void)validate . 我已经将NSToolbarItem子类化并实现了-(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. 我发现的引用说您应该能够通过调用[[[self view] window] firstResponder]来获得第一个响应者,然后查看响应者链是否实现了NSToolbarItemValidationNSUserInterfaceValidations并调用了适当的验证方法。

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. 但是,当我调用[[self view] window] ,我将nil作为窗口返回,因此无法检索第一响应者。

I'm not sure if it makes a difference, but I'm creating the toolbar in the XIB instead of writing it in code. 我不确定是否会有所作为,但我是在XIB中创建工具栏,而不是用代码编写工具栏。

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. 在NSToolbar.h中,有一个引用包含窗口的ivar,因此我可以通过调用[[[self toolbar] valueForKey:@"window"] firstResponder]来找到第一个响应者,但这有点[[[self toolbar] valueForKey:@"window"] firstResponder] ,我可以d倾向于使用记录在案的/更稳定的东西。

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. 对于需要验证的每个菜单项或工具栏,该应用程序在响应程序链中的每个对象上调用-validate(或其适当的变体),直到找到返回YES(表示它支持每个菜单或工具栏项的操作)的对象为止。 ),或找不到一个。 If it finds one, your toolbar item or menu item is enabled. 如果找到一个,则您的工具栏项或菜单项已启用。 If not, it is disabled. 如果不是,则将其禁用。 At least, that's the general theory. 至少,这是一般理论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM