iOS - passing arguments in action:@selector()

11,150

Solution 1

Why don't you make a Custom UIButton class and have the object as property?

See below.

"MyButton.h"

@interface MyButton : UIButton
@property(nonatomic, strong)MyClass *obj;
@end

"MyButton.m"

#import "MyButton.h"

@implementation MyButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

@end

Now assign MyButton class to your actual button in the cell/ or initialize the custom button instead of normal UIButton class and assign the object directly.

And in your IBAction where sender=MyButton

- (void) quantityDown:(id)sender{
   MyButton *btn  = (MyButton *)sender;
   //You get the object directly
   btn.obj;
}

Doing it this way you can actually access as many properties you want easily. And its useful in other implementations too.

Hope it helps.

Solution 2

Button can carry only one input so keep your sender and rowNum same so that it can be handled easily In cell for row method.

UIButton *b = [UIButton buttonWithType:UIButtonTypeContactAdd];
b.tag = indexPath.row;
[b addTarget:self action:@selector(quantityDown:) forControlEvents:UIControlEventTouchUpInside];

Your method

 - (void)quantityDown:(id)sender  
    {    
       NSLog(@"%d", sender.tag);  
    }

hope this will help...

Solution 3

Set each button tag as indexPath.row. Then just declare the function:

- (void)quantityDown:(id)sender

In that method do this:

UIButton *btn = (UIButton *)sender;

Add target like this:

[buttonDown addTarget:self action:@selector(quantityDown:) forControlEvents:UIControlEventTouchUpInside];

From btn.tag you can get the row number. Hope this helps. :)

Share:
11,150

Related videos on Youtube

Birrel
Author by

Birrel

Updated on June 04, 2022

Comments

  • Birrel
    Birrel almost 2 years

    I'm adding a button to a UITableViewCell programmatically. The method to be run when the button is pressed is - (void) quantityDown:(id)sender rowNumber:(int)rowNum, where rowNum is the row that the button appears in.

    When adding the target to the button, Xcode autocompletes the following:

    [buttonDown addTarget:self action:@selector(quantityDown:rowNumber:) forControlEvents:UIControlEventTouchUpInside];
    

    But no matter what I try, I cannot pass the row number into the method. I assumed the pertinent portion of code would look like

    action:@selector(quantityDown:rowNumber:indexPath.row)
    

    but that doesn't do the trick. I've seen other stuff like

    action:@selector(quantityDown:)rowNumber:indexPath.row
    

    and

    action:@selector(quantityDown:rowNumber:)withObject:@"first" withObject:@"Second"
    

    But neither work. I don't need to pass a first argument, just the row number. I've also tried defining the method like - (void) quantityDown:(int)rowNum and then writing the selector like:

    action:@selector(quantityDown:indexPath.row)
    

    but that also doesn't work.

    Thoughts?

    Thanks in advance.

    • Birrel
      Birrel about 10 years
      Just found this thread. Will try it out.
    • Birrel
      Birrel about 10 years
      Yep, that did it. I set buttonDown.tag = indexPath.row and then within the method I can access it via UIButton *clicked = (UIButton *)sender; and then setting an integer to clicked.tag
    • Vineesh TP
      Vineesh TP about 10 years
      You can't pass value in button click. possible ways are find value from button sender, or call another method on button click. 1st is the right method.
  • Birrel
    Birrel about 10 years
    Yep. That's what I ended up doing, as mentioned in a comment on the original question. Thanks!
  • Rashad
    Rashad about 10 years
    Your welcome. Yeah I see it now. May be I was busy in typing the answer.. :P
  • Birrel
    Birrel about 10 years
    Yeah, this is a nice solution. If I needed more data than just the row number, this would definitely be the way I'd go. The quick fix that I found is working for now, but it might not hold up to the rigors of everyday use. Great solution again!