iOS: button in UITableViewCell class to call method on main UIViewController

I have a UITableView on my MainViewController which has custom UITableCells defined in a CustomCell class. I need an UIButton, which is in my CustomCell class, to call a method in MainViewController. I cannot create a new instance of MainViewController because the method uses some variables which would all be in the default state if I created a new instance. What do I do??

This is my code:

MainViewController.m (this is the method I want called):

   double totalValue=0, personValue=0;
    [self returnTickArray];
    for(NSInteger i = 0; i < n; i++) {
        totalValue += ([[[self returnPricesArray] objectAtIndex:i] doubleValue] * [[[self returnQtyArray] objectAtIndex:i]doubleValue]);
        if([[[self returnPeopleArray] objectAtIndex:i]doubleValue]>0) personValue += ([[[self returnPricesArray] objectAtIndex:i] doubleValue] * [[[self returnQtyArray] objectAtIndex:i]doubleValue]/ [[[self returnPeopleArray] objectAtIndex:i]doubleValue] * [[[self returnTickArray] objectAtIndex:i]doubleValue]);
    _totalValue.text = [NSString stringWithFormat:@"$ %.02lf", totalValue];
    _tip.text= [NSString stringWithFormat:@"$ %.02lf", totalValue*(([_tipPercentage.text doubleValue]/100))];
    _addedValue.text= [NSString stringWithFormat:@"$ %.02lf",([[_tip.text substringFromIndex:2] doubleValue]+totalValue) ];
    _perPerson.text=[NSString stringWithFormat:@"$ %.02lf", personValue];

This is the method in CustomCell.m which gets called when I press the button:

- (IBAction)tick:(UIButton *)sender {

    if ([sender isSelected]) {
        [sender setImage:[UIImage imageNamed:@"off"] forState:UIControlStateNormal];
        [sender setSelected:NO];
        _isTicked = [NSNumber numberWithInt:0];
    else {
        [sender setImage:[UIImage imageNamed:@"on"] forState:UIControlStateSelected];
        [sender setSelected:YES];
        _isTicked = [NSNumber numberWithInt:1];



you can assign a method from your controller to a button from UITableView in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method

[cell.yourButton addTarget:self action:@selector(updateLabels) forControlEvents:UIControlEventTouchUpInside];

(just don't forget to add IBOutlet of your button to your custom cell class)

Need Your Help

How to Log a statement in j2me?

java-me lwuit

I developed an application in j2me, it is executing successfully. In future, if there is any issue in my statements, I want to debug my application. I know that, I can use System.out.println() but ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.