-1

I have a method with multiple variables:

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

I want to send NSString values (As this function is used for multiple elements).

This is how I add my action when no parameters are needed:

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

I tried adding parameters within the @selector like this:

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

But this does not work. How may I call my function with multiple parameters directly inside the @selector?

Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87
vadim
  • 119
  • 3
  • 14

2 Answers2

4

You can make your UIButton call a function in between (like a middle man), to control the next functions parameters.

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
}
Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87
D25
  • 283
  • 1
  • 4
  • 17
  • its sounds nice,what if i want to call to `showImg` from other method without button reference,can i replace the `(id)sender` with something unusable,`nil`? – vadim Oct 07 '12 at 16:32
  • Yes, you can remove the parameter and use @selector(showImg) and -(void) showImg – D25 Oct 07 '12 at 17:43
  • the code as you posted is working perfect for me, what i am asking is if i want to call to that method as written,but i don't have button to send for the sender,just want to call it from other place without changing it, can i do something like this `[self showImg:nil string1:@"32.5645" string2:@"32.35553"];` – vadim Oct 07 '12 at 19:52
0

The following line is wrong :

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

You can't pass parameters like this to the selector, that's why you have an error. I don't think you can pass multiple parameters to a selector. Maybe you can try to set a tag to your button with your const value (works with integers)

For example :

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

Just a suggestion.. :)

bs7
  • 627
  • 4
  • 11