如何定义一个像 NSButton 这样发送动作的类?

发布于 2024-09-17 06:31:29 字数 95 浏览 2 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(1

眉黛浅 2024-09-24 06:31:31
  1. 给它一个属性(非正式的也可以),其中包含目标的id。我不确定这是否应该保留;我会说不,因为目标通常是拥有(间接)拥有视图的窗口的控制器。
  2. 给它一个包含 SEL 的属性(最好是非正式的),用于操作。
  3. 通过向自己发送 accessibilityPerformAction: 来响应 mouseUp:keyDown:(检查相关按键是否为空格键或 Return 或 Enter)消息,传递NSAccessibilityPressAction
  4. 响应 accessibilityPerformAction: 消息,方法是将操作消息发送到目标 (NSAccessibilityPressAction) 或调用 super(其他),如该方法的文档中所述。

您还应该实现 NSAccessibility 协议的其余部分< /a> 当你在做的时候。结合使用辅助功能检查器和 VoiceOver 来测试其工作情况。

  1. Give it a property (informal is fine) holding an 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.
  2. Give it a property (informal in fine) holding a SEL, for the action.
  3. Respond to mouseUp: and keyDown: (checking that the key in question is the space bar or return or enter) by sending yourself an accessibilityPerformAction: message, passing NSAccessibilityPressAction.
  4. Respond to the accessibilityPerformAction: message by either sending your action message to your target (NSAccessibilityPressAction) or calling up to super (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.

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