如何定义一个像 NSButton 这样发送动作的类?
在 Cocoa 中,如何定义发送操作的类?我希望能够以 NSButton 的风格将操作连接到 IB 中另一个对象的选择器。如果可能的话,我不想继承 NSControl 的子类。
In Cocoa, how do you define a class that sends an action? I want to be able to connect the action to the selector of another object in IB in the style of NSButton. I would prefer not to subclass NSControl if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
id
。我不确定这是否应该保留;我会说不,因为目标通常是拥有(间接)拥有视图的窗口的控制器。SEL
的属性(最好是非正式的),用于操作。accessibilityPerformAction:
来响应mouseUp:
和keyDown:
(检查相关按键是否为空格键或 Return 或 Enter)消息,传递NSAccessibilityPressAction
。accessibilityPerformAction:
消息,方法是将操作消息发送到目标 (NSAccessibilityPressAction
) 或调用super
(其他),如该方法的文档中所述。您还应该实现 NSAccessibility 协议的其余部分< /a> 当你在做的时候。结合使用辅助功能检查器和 VoiceOver 来测试其工作情况。
id
, for the target. I'm not sure whether this should retain or not; I'd say no, since the target will normally be the controller that owns the window that (indirectly) owns the view.SEL
, for the action.mouseUp:
andkeyDown:
(checking that the key in question is the space bar or return or enter) by sending yourself anaccessibilityPerformAction:
message, passingNSAccessibilityPressAction
.accessibilityPerformAction:
message by either sending your action message to your target (NSAccessibilityPressAction
) or calling up tosuper
(other), as described in the documentation for that method.You should also implement the rest of the NSAccessibility protocol while you're at it. Test that work with a mix of the Accessibility Inspector and VoiceOver.