从碳代码调用可可ibaction
-
29-09-2019 - |
题
我想打电话给可可 IBAction
来自碳代码...
我已经设置了使用的全局键 本教程.
热键工作正常,但是当按下全局键时,我需要发射iBaction。
我使用时会遇到错误
[self functionName]
如何调用该功能?
我已经阅读了有关将可可控制器传递到碳方法的信息。我该怎么做?还是最好的方法是什么?
解决方案
我想你在打电话 [self functionName]
在碳活动处理程序回调中。那不是一个客观的方法,所以当然 self
没有定义。
安装碳事件处理程序时,参数之一是“用户数据”指针。您可以在此参数中传递Objective-C对象指针,以便您的活动处理程序可以得到它,您可以说类似的话 [(MyController*) inUserData functionName]
. 。当然,要使这项工作,您的处理程序必须在Objective-C或Objective-C ++源文件中。
其他提示
您可以将其中之一作为用户数据传递,同时确保程序安全地换取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;
};
不隶属于 StackOverflow