Selector function with int?

Using Cocos2d-iphone and objective-c game development framework.

I create a button with:

CCMenuItemImage *slot = [CCMenuItemImage itemFromNormalImage:@"BattleMoveSelectionSlot1.png" selectedImage:@"BattleMoveSelectionSlot2.png" target:self selector:@selector(moveChosen:i)]; 

And my moveChosen method:

 -(void)moveChosen:(int)index { } 

However, for some reason, I get the error @selector(moveChosen:i) , where I am an integer. How, for example, can I pass an integer parameter to my function when I click a button?

Error

Expected ':'

+4
source share
4 answers

The argument names are not included in the selector:

 @selector(moveChosen:) 

Selectors do not allow parameter binding.

+4
source
Georg is right. Note that, as implemented, this will cause undefined behavior, since index is int , but the action method that it uses expects an object ( id ) there, not an int . Action Method Signature:
 - (void)methodName:(id)sender; 

Or when used with Interface Builder:

 - (IBAction)methodName:(id)sender; 

( IBAction is an alias for void . The two are semantically different, but functionally identical.)

Where sender is the object that sent the action message - in this case, the object you created and assigned to the slot variable.

+4
source
Georg is partially right. For your example, this would be:
 @selector(moveChosen:) 

But note: if you have more than one parameter, you include formal parameter names to get a selector. If your signature is a function:

 - (void)moveChosen:(int)index withThing:(Thing*)thing 

then the selector will be:

 @selector(moveChosen:withThing:) 
+2
source

A selector is simply the name of the message you want to send. Arguments will be provided when it is called - this means that CCMenuItemImage will decide which argument is passed. If CCMenuItemImage does not support providing an integer parameter, you cannot do this.

+1
source

All Articles