Iphone perfomSelector on按钮和发送参数

Iphone perfomSelector on按钮和发送参数,iphone,objective-c,selector,Iphone,Objective C,Selector,我有一个多变量的方法: -(void) showImg:(UIBarButtonItem *)sender string1:(NSString *) string2:(NSString *); 我想发送NSString值(因为此函数用于多个元素) 这就是我在不需要参数时添加我的操作的方式: [myButton addTarget:self action:@selector(showImg) forControlEvents: UIControlEventTouchUpInside]; 我尝试

我有一个多变量的方法:

-(void) showImg:(UIBarButtonItem *)sender string1:(NSString *) string2:(NSString *);
我想发送
NSString
值(因为此
函数
用于多个元素)

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

[myButton addTarget:self action:@selector(showImg) forControlEvents: UIControlEventTouchUpInside];
我尝试在
@选择器中添加参数,如下所示:

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

但这是行不通的。如果在
@选择器
中直接包含多个参数,如何调用我的
函数

以下行错误:

[myButton performSelector@selector(showImg:string1:string2::)with object:@“-1”with object:@“-1”]

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

例如:

//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;
}

只是一个建议……)

您可以让您的
ui按钮
在两者之间调用一个
函数
(如中间人),以控制下一个函数参数

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
}
UIButton*myButton=[UIButton按钮类型:uiButtontyPeroundRect];
[myButton addTarget:self action:@selector(buttonTapped:)for ControlEvents:UIControlEventTouchUpInside];
-(无效)按钮:(id)发送者{
if(){
[自显示:发送方字符串1:@“-1”字符串2:@“-1”];
}否则{
[自显示:发送方字符串1:@“otherVal1”字符串2:@“otherVal2”];
}
}
-(void)showImg:(id)发送方字符串1:(NSString*)字符串1:(NSString*)字符串2:(NSString*)字符串2{
//其他逻辑
}

听起来不错,如果我想在没有按钮引用的情况下从其他方法调用
showImg
,我可以将
(id)发送方
替换为不可用的,
nil
?是的,您可以删除参数并使用@selector(showImg)和-(void)showimgt您发布的代码对我来说非常适合,我想问的是,如果我想按编写的方式调用该方法,但我没有发送发件人的按钮,只是想从其他地方调用它而不更改它,我可以这样做吗
[self showImg:nil string1:@“32.5645”string2:@“32.35553”]