Question

I am using cocos2d for developing. I have tried to wrap a customized button class. When trying to make the button respond to the selector I assigned to it, I used NSInvocation. In it, the MyButton works like this.

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

I have passed the sel a function like this:

- (void)onButtonClicked:(id)sender;

my question is: does it necessary to add this line [invocation_ setArgument:&self atIndex:2]; in MyButton's implementation?

I have this question is because according to ios documentation here: NSInvocation Class Reference

it addressed that:

Indices 0 and 1 indicate the hidden arguments self and _cmd, respectively; you should set these values directly with the setTarget: and setSelector: methods. Use indices 2 and greater for the arguments normally passed in a message.

it seems that self has been passed when calling setTarget, does it mean that, the &self argument is not necessary to be passed in setArgument method?

Thanks

Was it helpful?

Solution

The documentation just tell that the first argument (with indice 0) represent the target object (so the "self"). As the documentation explains you the first argument is set using the setTarget: method.

You need to keep this line of code [invocation_ setArgument:&self atIndex:2]; to send the button's reference to the target-action pair.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top