2010-11-07 15 views
0

我試圖從碳代碼調用可可IBAction調用可可IBAction爲...從活性炭標號

我已經建立了使用this tutorial全局鍵。

熱鍵工作正常,但我需要在全局鍵被按下時觸發一個IBAction。

我不斷收到錯誤,當我使用

[self functionName] 

如何調用該函數?

我已閱讀關於將可可控制器傳遞給碳方法。我將如何做到這一點?或者什麼是最好的方法?

回答

1

我假設你在Carbon事件處理函數回調中調用[self functionName]。這不是一個Objective-C方法,所以當然沒有定義self

當您安裝碳事件處理程序時,其中一個參數是「用戶數據」指針。你可以在這個參數中傳遞一個Objective-C對象指針,這樣你的事件處理程序就可以得到它,你可以說類似於[(MyController*) inUserData functionName]。當然,要做到這一點,您的處理程序必須位於Objective-C或Objective-C++源文件中。

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; 
};