3

I created an action in

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event

which just like this:

self.moveAction = [CCSequence actions:                          
                   [CCMoveTo actionWithDuration:moveDuration position:touchLocation],
                   [CCCallFunc actionWithTarget:self selector:@selector(guyMoveEnded)],
                   nil
                  ];

but now, I want to auto-invoke a following method by @selector:

-(void)guyMoveEnded:(BOOL)flag AndWhere:(CGPoint)where Andtime:(float)time{
    //do something...
}

How can I do it? Please help me, I'm confused about the selector.

Thanx!

coverback
  • 4,413
  • 1
  • 19
  • 31
smileVann
  • 554
  • 1
  • 6
  • 15

4 Answers4

10

You could add your parameters to a NSArray and then give it as the argument to your selector:

NSArray *params = [NSArray arrayWithObjects:@"a str", [NSNumber numberWithInt:42],myObj];
[self performSelector:@selector(myMethod:)withObject:params];

And then unpack the arguments in your method:

-(void)myMethode:(NSArray*)params
{
    NSString *strArg = [params objectAtIndex:0];
    NSNumber * numVal = [params objectAtIndex:1];
    NSObject *objArg = [params objectAtIndex:2];
    int intArg = [numVal intValue];
    .
    .
    .   
}
EsbenB
  • 3,356
  • 25
  • 44
3

Apple suggests using NSInvocation when you need to pass more than 2 arguments. Here's a very good example: NSInvocation for Dummies?

A short synopsis from that answer with no explanations:

// Prepare the object
NSMethodSignature * mySignature = [NSMutableArray 
    instanceMethodSignatureForSelector:@selector(addObject:)];
NSInvocation * myInvocation = [NSInvocation
    invocationWithMethodSignature:mySignature];

// Set selector and object
[myInvocation setTarget:myArray];
[myInvocation setSelector:@selector(addObject:)];

// Set arguments
[myInvocation setArgument:&myString atIndex:2];

// Invoke it
[myInvocation invoke];

It is a lot more code than just calling a selector with performSelector:, but allows you to call methods with any number of arguments. This is especially useful if you can't change the signature to decrease argument count, as Cipramill suggests.

Community
  • 1
  • 1
coverback
  • 4,413
  • 1
  • 19
  • 31
1

Prepare a NSDictionary object with your Parameters & pass that Dictionary to your selector & you can get the values inside your method from Dictionary. following is for reference:

 "NSDictionary *dictionary = nil;
  BOOL flag = YES;
  CGFloat time;
  CGPoint pt;
 [dictionary setValue:flag forKey:FIRSTPARAM];
  [dictionary setValue:time forKey:SECONDPARAM];
  [dictionary setObject:point forKey:THIRDPARAM];
[self performSelector:@selector(methodName:) withObject:dictionary];

"

Either You can create your custom class for that Pass the object of that class.

Piyush
  • 244
  • 1
  • 2
  • 8
  • your answer is the most detailed ,thanks so much,i'll try it. – smileVann Jul 27 '12 at 07:59
  • 1
    In my opinion, using a dictionary is better than an array because each object can be 'named' meaningfully (by its key). Of course, you could use an `enum` with meaningful names for the indices of the array... – Nicolas Miari Jul 27 '12 at 08:06
-1
[favProductCell.btnAddtoCart addTarget:self action:@selector(arrayForAddToCart:) forControlEvents:UIControlEventTouchUpInside];

[favProductCell.btnAddtoCart setTag:indexPath.row];

-(void)arrayForAddToCart:(id)sender
{
    if (![self.addToCartProductIdArray containsObject:self.favoriteProductArray[[sender tag]]])
        [self.addToCartProductIdArray addObject:self.favoriteProductArray[[sender tag]]];
}
RedBaron
  • 4,717
  • 5
  • 41
  • 65
Chigs79
  • 162
  • 4