【发布时间】:2012-07-25 03:44:17
【问题描述】:
我正在使用 cocos2d 进行开发。我试图包装一个自定义的按钮类。当试图让按钮响应我分配给它的选择器时,我使用了 NSInvocation。在其中,MyButton 是这样工作的。
if( target && sel ) {
sig = [target methodSignatureForSelector:sel];
invocation_ = nil;
invocation_ = [NSInvocation invocationWithMethodSignature:sig];
[invocation_ setTarget:target];
[invocation_ setSelector:sel];
[invocation_ setArgument:&self atIndex:2];
[invocation_ retain];
}
我已经向 sel 传递了一个这样的函数:
- (void)onButtonClicked:(id)sender;
我的问题是:是否有必要在 MyButton 的实现中添加 [invocation_ setArgument:&self atIndex:2]; 这一行?
我有这个问题是因为根据这里的 ios 文档: NSInvocation Class Reference
它指出:
索引 0 和 1 表示隐藏参数 self 和 _cmd, 分别;您应该直接使用 setTarget 设置这些值: 和 setSelector: 方法。使用索引 2 和更大的参数 通常在消息中传递。
调用setTarget时好像传了self,是不是说setArgument方法中不需要传入&self参数?
谢谢
【问题讨论】:
-
在这种情况下
self是什么?在哪里创建调用? -
@JoshCaswell 是我自己实现的 CCNode 的子类 MyButton。在上下文中,self 表示将它“自己”的按钮视为“发送者”。
标签: ios xcode arguments nsinvocation