I have a UITableView
on my MainViewController
which has custom UITableCell
s 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):
-(void)updateLabels{
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)
Handling changes, actions, or user interaction in other views is the essential use case of delegates . The best practice is to have the MainViewController
be the delegate of the CustomCell
, and then as events happen in the cell (ie, tick
is called), it calls certain methods on its delegate to notify it and the delegate then calls updateLabels
.
Other options for keeping values/state in sync between views are:
NSNotification
s sent through NSNotificationCenter
- best used for actions interesting :) You need to use [UIApplication sharedApplication]
singleton to process this. Here are few steps:
Create object of MainViewController
In you AppDelegate
class:
in AppDelegate.h
import MainViewController.h
@interface AppDelegate
{
MainViewController *mvc;
}
@property (strong) MainViewController *mvc;
in MainViewController.m
@implementation AppDelegate
@synthesize mvc =_mvc
Now come to your CustomCell.m
- (IBAction)tick:(UIButton *)sender {
[[(AppDelegate *)[[UIApplication sharedApplication] delegate] hvc] tick:sender];
}
Here you are seeing a method is called- tick_fromCell:
. Now you have to define this method in your MainViewController
class' .h
ans .m
files.
in MainViewController.h
-(void)tick:(id)sender;
and in MainViewController.m
-(void)tick:(id)sender {
NSLog("Clicking from my CustomCell :)");// Here you will put updateLabels method
[self updateLabels];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.