1

NSInvocationを使用してオブジェクトのメソッドを呼び出し、をsender引数として送信しようとしています。以下のコードはmthodを呼び出しますが、オブジェクトがmthodに渡されているようですが、実際には自己オブジェクトではありません

- (void)setTarget:(id)taret withAction:(SEL)selector
{
    NSMethodSignature *methodSignature = [target methodSignatureForSelector:action];
    _invocation = [[NSInvocation invocationWithMethodSignature:methodSignature] retain];

    _invocation.target = target;
    _invocation.selector = action;
    [_invocation setArgument:self atIndex:2];
}

- (void)callTargetWithSender
{
   [_invocation invoke];
}
4

3 に答える 3

2

『分散オブジェクトプログラミングガイド』の「NSInvocationの使用」を参照してください。


新しい質問に基づいて編集*

私は上記がこのように呼ばれると思います:

[foo setTarget:target withAction:@selector(doSomething:)];

その場合、最終的なメッセージは次のようになります。

[target doSomething:foo];
于 2011-07-19T02:56:10.487 に答える
2

[呼び出しsetArgument:(__ bridge void *)(self)atIndex:2];

于 2014-01-09T09:27:58.343 に答える
1

使ってみませんか

   [target performSelector:selector withObject:self];

??? の場合、これは次のようになりますselector@selector(foo:)

   [target foo:self];

NSInvocation私の意見では、あなたの状況ではやり過ぎです。

于 2011-07-19T04:39:41.090 に答える