0

IBActionCarbon コードからCocoa を呼び出そうとしています...

このチュートリアルを使用してグローバル キーを設定しました。

ホット キーは正常に機能していますが、グローバル キーが押されたときに IBAction を起動する必要があります。

使用するとエラーが発生し続けます

[self functionName]

関数を呼び出すにはどうすればよいですか?

Cocoaコントローラーをカーボンメソッドに渡すことについて読んだことがあります。どうすればいいですか?または最善の方法は何ですか?

4

2 に答える 2

1

[self functionName]Carbonイベントハンドラーコールバックを呼び出していると仮定します。これはObjective-Cメソッドでselfはないため、もちろん定義されていません。

Carbon Eventハンドラーをインストールする場合、パラメーターの1つは「ユーザーデータ」ポインターです。このパラメーターでObjective-Cオブジェクトポインターを渡すと、イベントハンドラーがそれを取得し、のように言うことができます[(MyController*) inUserData functionName]。もちろん、これを機能させるには、ハンドラーがObjective-CまたはObjective-C++ソースファイルに含まれている必要があります。

于 2010-11-07T19:47:23.787 に答える
0

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;
};
于 2010-11-07T20:10:55.000 に答える