Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selector function with int parameter?

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 is:

-(void)moveChosen:(int)index {

}

However, for some reason I get an error on @selector(moveChosen:i) where i is an integer. How, then, may I pass an integer parameter to my function when the button is pressed?

The error is

Expected ':'

like image 817
Voldemort Avatar asked Dec 27 '22 14:12

Voldemort


2 Answers

Georg is correct. Note that as implemented, this will invoke undefined behaviour since index is an int but the action method it's being used as expects an object (id) there, not an int. The signature of an action method is:

- (void)methodName:(id)sender;

Or, when used with Interface Builder:

- (IBAction)methodName:(id)sender;

(IBAction is an alias of 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.

like image 160
Jonathan Grynspan Avatar answered Dec 30 '22 04:12

Jonathan Grynspan


You don't include any argument names in the selector:

@selector(moveChosen:)

Selectors don't allow for binding parameters.

like image 45
Georg Fritzsche Avatar answered Dec 30 '22 02:12

Georg Fritzsche