从 Carbon Code 调用 Cocoa IBAction

发布于 2024-10-01 05:04:42 字数 373 浏览 4 评论 0原文

我正在尝试从 Carbon 代码调用 Cocoa IBAction...

我已经使用 本教程

热键工作正常,但我需要在按下全局键时触发 IBAction。

时,我不断收到错误消息

[self functionName]

当我使用“如何调用该函数?”

。我读过有关将 Cocoa 控制器传递给 Carbon 方法的内容。我该怎么做?或者什么是最好的方法?

I'm trying to call a Cocoa IBAction from Carbon code...

I've set up global keys using this tutorial.

The hot keys are working fine, but I need to fire an IBAction when the global key has been pressed.

I keep getting errors when I use

[self functionName]

How do I call the function?

I've read about passing the Cocoa controller to the carbon method. How would I do this? or what is the best way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

流星番茄 2024-10-08 05:04:42

我假设您在 Carbon 事件处理程序回调中调用 [self functionName] 。这不是 Objective-C 方法,所以当然没有定义 self 。

当您安装 Carbon 事件处理程序时,参数之一是“用户数据”指针。您可以在此参数中传递一个 Objective-C 对象指针,以便您的事件处理程序能够获取它,您可以说类似 [(MyController*) inUserData functionName] 的内容。当然,要使其工作,您的处理程序必须位于 Objective-C 或 Objective-C++ 源文件中。

I assume you're calling [self functionName] in a Carbon Event handler callback. That's not an Objective-C method, so of course self is not defined.

When you install a Carbon Event handler, one of the parameters is a "user data" pointer. You can pass an Objective-C object pointer in this parameter, so that your event handler will get it, and you can say something like [(MyController*) inUserData functionName]. Of course, to make this work, your handler must be in an Objective-C or Objective-C++ source file.

So要识趣 2024-10-08 05:04:42

您可以将其中之一作为用户数据传递,同时保持程序对于 C++ 翻译的安全:

/* include the necessary C header, located in objc/ (objc/objc.h?) */

/* of course, definitions with objc messaging belong in your .mm file */

class t_ibaction_invocation {

/* you may want to retain d_target or d_optionalArgument, and release at destruction */
    enum { RetainArguments = 0 };
public:

/* IBAction takes the form: [target action:optionalArgument]; */

    t_ibaction_invocation(id target, SEL action, id optionalArgument) : d_target(target), d_action(action), d_optionalArgument(optionalArgument) {
        assert(this->d_target);
        if (RetainArguments) {
            [this->d_target retain];
            [this->d_optionalArgument retain];
        }
    }

    ~t_ibaction_invocation() {
        if (RetainArguments) {
            [this->d_target release], target = 0;
            [this->d_optionalArgument release], optionalArgument = 0;
        }
    }

    id performAction() {
        if (this->d_target && this->d_action) {
            return [this->d_target performSelector:this->d_action withObject:this->d_optionalArgument];
        }
        else {
            assert(d_target && d_action);
            return 0;
        }
    }

private:
    id d_target;
    SEL d_action;
    id d_optionalArgument;
};

you can pass one of these as your user data while keeping the program safe for c++ translations:

/* include the necessary C header, located in objc/ (objc/objc.h?) */

/* of course, definitions with objc messaging belong in your .mm file */

class t_ibaction_invocation {

/* you may want to retain d_target or d_optionalArgument, and release at destruction */
    enum { RetainArguments = 0 };
public:

/* IBAction takes the form: [target action:optionalArgument]; */

    t_ibaction_invocation(id target, SEL action, id optionalArgument) : d_target(target), d_action(action), d_optionalArgument(optionalArgument) {
        assert(this->d_target);
        if (RetainArguments) {
            [this->d_target retain];
            [this->d_optionalArgument retain];
        }
    }

    ~t_ibaction_invocation() {
        if (RetainArguments) {
            [this->d_target release], target = 0;
            [this->d_optionalArgument release], optionalArgument = 0;
        }
    }

    id performAction() {
        if (this->d_target && this->d_action) {
            return [this->d_target performSelector:this->d_action withObject:this->d_optionalArgument];
        }
        else {
            assert(d_target && d_action);
            return 0;
        }
    }

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