0

I have a number like 12345678910111213 and I need to pass it from one method(cellForRow) to another(button action method). The simplest way which I used to use is to pass it through a button tag. In this case it is impossible(?). I can also create a property for it but what about encapsulation? I want to know really RIGHT(and preferably simple) way for doing things like that. Thanks in advance!

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
Stas
  • 9,925
  • 9
  • 42
  • 77

4 Answers4

1

Well you can actually attach the value to the UIButton. When you have the value you want to pass and you have a reference to the button:

static char kMyObject;

objc_setAssociatedObject(myButton, &kMyObject, [NSNumber numberWithInt:myInt], OBJC_ASSOCIATION_RETAIN_NONATOMIC);

On the other side, when you receive the action with the button as id:

- (void)myAction:(id)sender
{
   UIButton *myButton = (UIButton*)sender;
   NSNumber *number=objec_getAssociatedOject(myButton,&kMyObject);
}
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • @Stas I am just explain how can you pass the value. How you actually populate your cells and how you organize the numbers, is up to you... – Rui Peres May 11 '12 at 12:42
  • Thanks, i simply don't understand what you are doing ))) I'll try it. Is objc_setAssociatedObject some C function, or what ? – Stas May 11 '12 at 13:01
  • Check this: http://stackoverflow.com/questions/5909412/what-is-objc-setassociatedobject-and-in-what-cases-should-it-be-used – Rui Peres May 11 '12 at 13:09
  • Create a category on `UIButton` to wrap the calls to `objc_setAssociatedObject()` and `objec_getAssociatedOject()` with a property. – JeremyP May 11 '12 at 13:29
0

In this integer tag you can store a pointer (case of 32 bit address) to a class/structure/whatever what represents bigint.

For example:

UIButton *button = [UIButton ...];
button.tag = (int)[[MyBigInt alloc] initWithString:@"12131312312312312"];

after:

MyBigInt *bigInt = (MyBigInt *)button.tag;
...
[bigInt release];
Eldar Markov
  • 950
  • 9
  • 13
0

You cannot pass it as a tag as Saad said. You can use NSDecimal numbers here. @Saad cannot use double, as it will lose precision.

zahreelay
  • 1,742
  • 12
  • 18
0

I'm going to make some assumptions here, because I just when through something similar.

  1. The UIButton with the action is in a UITableViewCell.
  2. You have an underlying source for all your data (ie. An array with all your data in it).
  3. You have easy access to your tableView.

First, you need to get the cell which contains the button:

UITableViewCell *cell = nil;
for (UIView *view = sender; view; view = view.superview) {
    if ([view isKindOfClass:[UITableViewCell class]]) {
        cell = (UITableViewCell *)view;
        break;
    }
}

Next, you need to get the indexRow for that cell:

NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

Finally, you have to get access to your data:

ModelClass modelObject* obj = [self.data objectAtIndex:indexPath.row];

Now, you can make any changes you need to your model.

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • Sorry it's not so simple, but as far as I can tell, this is the proper way to get access to the model from within a table view cell. I created a category for UIView that has a method -cellSuperview to do the first part. I then created a method -objectForSender: in the controller so I don't have to write that code for each action. – Jeffery Thomas May 11 '12 at 14:17