21

How do I add custom data while specifying a target in a UIButton?

id data = getSomeData();
[button addTarget:self 
           action:@selector(buyButtonTapped:event:) 
 forControlEvents:UIControlEventTouchUpInside];

I want the buyButtonTapped function to look like:

(void) buyButtonTapped: (UIButton *) button event: (id) event data: (id) data
Gautam
  • 2,079
  • 4
  • 17
  • 17

3 Answers3

67

not possible. the action that is triggered by an UIControl can have 3 different signatures.

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event

None of them allows you to send custom data.


Depending on what you want to do you can access the data in different ways.

For example if the button is in a UITableView you could use something like this:

- (IBAction)buttonPressed:(UIButton *)sender {
    CGPoint buttonOriginInTableView = [sender convertPoint:CGPointZero toView:tableView];
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:buttonOriginInTableView];
    NSData *customData = [myDataSource customDataForIndexPath:indexPath];
    // do something
}

There is always a way to get the data without passing it to the button.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • 1
    That works. Thanks! It is indeed a custom button in a UITableView.The data I wanted was the UITableView itself to find out which button was clicked. – Gautam Apr 16 '11 at 23:52
  • 6
    It's always a tableview ^^ – Matthias Bauch Apr 17 '11 at 02:05
  • 1
    "THere is always a way..." are you sure? – user102008 Apr 23 '11 at 01:05
  • 3
    yes I am. Feel free to challenge me. But I have to admit, sometimes it takes some ugly workarounds or adding more instance variables. – Matthias Bauch Apr 23 '11 at 01:24
  • it didn't work for me - so I had to change the second line to this: UITableViewCell *cell = (UITableViewCell *)contentView; – Amit Hagin Sep 28 '12 at 10:21
  • What if you have multiple buttons in each cell? How do you separate them? – Michael Feb 22 '13 at 16:22
  • 1
    @Michael you could call a different IBAction for each button in the cell. So if you have an up button and a down button, for example, you could have a function upButtonPressed for when the up button is pressed, and a function downButtonPressed for when the down button is pressed. – Laura Mar 20 '13 at 19:58
  • also works for collection views, thanks really innovative solution – dreampowder Feb 20 '14 at 13:01
  • What's happens if button clicked in a row, which was replaced by another row due to dataset updates/tableView reloads ? I think this is a bad solution in a tables which is frequently receive updates for a dataset – A. Petrov Feb 05 '16 at 09:54
21

You cannot send extra data to the action method. There are a number of ways to associate the data with the button, although none are particularly straightforward unless the data is a single NSInteger.

  • You can use the tag property to hold a single NSInteger. This may be all you need, or you could use it to look up an object in an array or dictionary.
  • You can subclass UIButton to add ivars/properties to store your needed data.
  • You can use [NSValue valueWithNonretainedObject:button] as a key for a dictionary.
  • My personal favorite for one-offs, you can use [associative references] to associate the data object with the button.
Anomie
  • 92,546
  • 13
  • 126
  • 145
  • 4
    subclassing UIButton definitely does the trick and should be one of the correct answers imho – the_critic Apr 29 '13 at 15:24
  • 1
    tag ended up being all I needed and saved me some time :) Otherwise, I would have subclassed. This is the better answer IMO – Charlie Martin Nov 03 '14 at 07:30
  • Option 4 makes no sense: You have to have some kind of selector to pass on to objc_setAssociatedObject, which NSHipster solves using a category, so why use opt. 4 at all? –  Aug 24 '15 at 17:40
-3

You can't really do that. What you can do is put the data in a dictionary and use the button to get it later.

E.g.

myDataDict = [NSDictionary dictionaryWithObjectsAndKeys:someData, button, nil];

Then later;

-(void) buttonPress:(id)sender
{
  data = [dataDict objectForKey:sender];
}

If your buttons are specified in InterfaceBuilder you can use the 'tag' property of a button to lookup the data, although you will need to convert it to an NSNumber for use with the dictionary.

Andrew Grant
  • 58,260
  • 22
  • 130
  • 143
  • Note that using the button itself as the dictionary key will copy the button object, at which point it will probably no longer compare equal to the original. And the `tag` property can be used with or without IB. – Anomie Apr 16 '11 at 23:42
  • @Anomie: in fact, I don't even think buttons can be copied, because they do not conform to `NSCopying` – user102008 Apr 23 '11 at 01:06