smileVann
smileVann

Reputation: 554

How to pass 3 parameters to @selector

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!

Upvotes: 3

Views: 4771

Answers (4)

Chigs79
Chigs79

Reputation: 162

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

Upvotes: -1

coverback
coverback

Reputation: 4413

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.

Upvotes: 3

EsbenB
EsbenB

Reputation: 3406

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

Upvotes: 10

Piyush
Piyush

Reputation: 244

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.

Upvotes: 1

Related Questions