Stas
Stas

Reputation: 9935

Working with big numbers

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!

Upvotes: 0

Views: 156

Answers (4)

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

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.

Upvotes: 0

Eldar Markov
Eldar Markov

Reputation: 950

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];

Upvotes: 0

zahreelay
zahreelay

Reputation: 1742

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.

Upvotes: 0

Rui Peres
Rui Peres

Reputation: 25917

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

Upvotes: 1

Related Questions