-1

我有一个包含多个变量的方法:

-(void) showImg:(UIBarButtonItem *)sender string1:(NSString *) string2:(NSString *);

我想发送NSString值(因为这function用于多个元素)。

这就是我action在不需要参数时添加的方式:

[myButton addTarget:self action:@selector(showImg) forControlEvents: UIControlEventTouchUpInside];

我尝试在@selector这样的范围内添加参数:

[myButton performSelector @selector(showImg:string1:string2::) withObject:@"-1" withObject:@"-1"];

但这不起作用。如何function直接在内部调用带有多个参数的my @selector

4

2 回答 2

4

您可以在两者之间进行UIButton调用function(如中间人),以控制下一个函数参数。

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

-(void) buttonTapped:(id)sender{
    if(<some logical condition>){
        [self showImg:sender string1:@"-1" string2:@"-1"];
    }else {
        [self showImg:sender string1:@"otherVal1" string2:@"otherVal2"];
    }
}

-(void) showImg:(id)sender string1:(NSString *)string1 string2:(NSString*)string2 {
    //Other logic
}
于 2012-10-06T21:17:04.807 回答
0

以下行是错误的:

[myButton performSelector @selector(showImg:string1:string2::) withObject:@"-1" withObject:@"-1"];

您不能将这样的参数传递给选择器,这就是您出错的原因。我认为您不能将多个参数传递给选择器。也许您可以尝试使用 const 值为按钮设置标签(适用于整数)

例如 :

//Init your button......
[myButton setTag:1];
[myButton addTarget:self action:@selector(showImg:) forControlEvents: UIControlEventTouchUpInside];

- (void)showImg:(id)sender {
  UIButton *btn = (UIButton *)sender;
  int value = btn.tag;
}

只是一个建议.. :)

于 2012-10-06T21:13:22.197 回答