在 Objective-C 中处理 Cocoa setAction 消息
如何在 Objective-C++ 中处理 setAction
消息? (不是 Objective-C。)
例如,假设我有:
my_class.mm
NSSegmentedControl *segmented = [[NSSegmentedControl alloc] init];
[segmented setSegmentCount:5];
// etc.
[segmented setAction:???];
应用程序:我正在使用 Qt (C++) 进行编程,并且需要一个围绕一些 Cocoa 小部件的包装器想直接使用。我继承自 QMacCocoaViewContainer,但无法弄清楚如何处理我正在包装的 NSSegmentedControl 的“点击”。最终这将发出一个标准的 Qt 信号。
How do you handle the setAction
message in Objective-C++? (Not Objective-C.)
For example, suppose I have:
my_class.mm
NSSegmentedControl *segmented = [[NSSegmentedControl alloc] init];
[segmented setSegmentCount:5];
// etc.
[segmented setAction:???];
Application: I am programming in Qt (C++) and need a wrapper around some Cocoa widgets I want to use directly. I am inheriting from QMacCocoaViewContainer
but can't figure out how to handle the "clicks" of the NSSegmentedControl
I am wrapping. Eventually this will emit a standard Qt signal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
action
只是一个选择器 - 它与target
一起使用。因此,为 target+action 编写一个 objc 方法,它可以调用或执行您真正想要的操作。actions
' 参数是发送者,但如果不需要,可以忽略它。发送者将是发送消息的任何东西(例如控件)。在 ObjC++ 中没有什么不同 - 这必须包装在 objc 方法中,因为目标必须是 objc 对象。所以它看起来像这样:
方法是:
action
is just a selector - it is used in tandem withtarget
. so write an objc method for target+action which calls through or does what you really want.actions
' arguments are the sender, but you can omit that if you don't need it. the sender will be whatever is sending the message (e.g. the control). it's no different in ObjC++ - this has to be wrapped in an objc method because the target must be an objc object.so it would look like this:
and the method is:
@Justin 有正确的答案;我会接受它,但也会包括最终的解决方案,以防它对其他人有帮助。诀窍是您需要一个代理类,正如 @smparkes 所指出的。
为了简洁起见,忽略
.h
文件:mac_control.mm
mac_control_proxy.mm< /强>
@Justin has the right answer; I'll accept it, but also include the final solution in case it helps others. The trick is you need a proxy class, as @smparkes noted.
Ignoring the
.h
files for brevity:mac_control.mm
mac_control_proxy.mm
我正在跟进戴夫·马特尔的回答(这非常有帮助!)。
我在设置 C++ 目标(从 Objective-C++ 类中)时遇到问题,并使用 [NSValue valueWithPointer:theTargetCxxClass] 将目标传递给 Proxy.mm 类。
因此,在我的 Objective-C++ 类中,而不是这样做:
我这样做了:
或者
这样做消除了将 C++ 类(不扩展类型“id”)传递给 Objective-C++ 代理类的错误。
在代理类内部,您需要使用 NSValue 的 pointValue 方法,然后强制转换回 C++ 类,以便向其发送消息。
我首先注意到 这篇文章。
I'm following up on Dave Mateer's answer (which was super helpful!).
I was having issues setting the C++ target (from within a objective-C++ class) and used [NSValue valueWithPointer:theTargetCxxClass] to pass the target to the Proxy.mm class.
So, inside of my Objective-C++ class, rather than doing this:
I did this:
or
And doing this got rid of an error about passing a C++ class (which does not extend type "id") to the Objective-C++ proxy class.
Inside of the proxy class, you then need to use NSValue's pointerValue method and then cast back to the C++ class in order to send a message to it.
I first was alerted to the "valueWithPointer" trick in this post.