通过performSelector ios调用多个参数的方法

时间:2015-03-14 15:23:38

标签: ios objective-c performselector

这是我的方法,有两个参数:imgArrayandDurationArray。 我想在else部分performSelector中使用相同的两个参数调用相同的方法。我怎么称呼它?非常感谢。

-(void) imageAnimationWithImage:(NSArray *) imgArray andDurationArray:(NSArray *) durationArr
{
    if (count == imgarray.count)
    {
        count = 0;
        [self imageAnimationWithImage:imgarray andDurationArray:durationArr];
    }
    else
    {
        emoImageView.image = [UIImage imageNamed:[imgarray objectAtIndex:count]];
        [self performSelector:@selector(imageAnimationWithImage:andDurationArray:) withObject:imgarray withObject:durationArr afterDelay:[[durationArr objectAtIndex:count] doubleValue]];
        count++;
    }
}

3 个答案:

答案 0 :(得分:3)

为什么不使用NSInvocation它更适合多个参数,第二个注意你正在创建一个具有高递归风险的方法

  NSInteger delay = [[durationArr objectAtIndex:count] doubleValue];
  SEL aSelector = @selector(imageAnimationWithImage:andDurationArray:);
  NSMethodSignature *signature = [self methodSignatureForSelector:aSelector];
  NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
  [invocation setTarget:self];
  [invocation setSelector:aSelector];
  [invocation setArgument:&imgarray atIndex:2];
  [invocation setArgument:&durationArr atIndex:3];

  [invocation performSelector:@selector(invoke) withObject:nil afterDelay:delay];

答案 1 :(得分:3)

使用dispatch_after。它更容易:

double delay = [[durationArr objectAtIndex:count] doubleValue];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [self imageAnimationWithImage:imgarray andDurationArray:durationArr];
});

答案 2 :(得分:1)

方法performSelector:withObject:afterDelay仅适用于0或1个参数。您可以将参数打包到字典中并传递它,或使用dispatch_after继承封闭范围,因此不需要参数。