如何将3个参数传递给@selector

时间:2012-07-27 07:22:00

标签: iphone objective-c ios

我在

中创建了一个动作
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event

就像这样:

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

但是现在,我想通过@selector自动调用以下方法:

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

我该怎么办? 请帮助我,我对选择器感到困惑。

感谢名单!

3 个答案:

答案 0 :(得分:10)

您可以将参数添加到NSArray,然后将其作为参数提供给选择器:

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

然后解压缩方法中的参数:

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

答案 1 :(得分:1)

使用您的参数&准备一个NSDictionary对象。将该词典传递给您的选择器&你可以从Dictionary中获取方法中的值。以下是供参考:

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

"

你可以为那个Pass的对象创建自定义类。

答案 2 :(得分:-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]]];
}