iphone - iOS: button in UITableViewCell class to call method on main UIViewController -
i have uitableview
on mainviewcontroller
has custom uitablecell
s defined in customcell
class. need uibutton
, in customcell
class, call method in mainviewcontroller
. cannot create new instance of mainviewcontroller
because method uses variables in default state if created new instance. do??
this code:
mainviewcontroller.m
(this method want called):
-(void)updatelabels{ double totalvalue=0, personvalue=0; [self returntickarray]; for(nsinteger = 0; < 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 method in customcell.m
gets called when press 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 method controller button uitableview in - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
method
[cell.yourbutton addtarget:self action:@selector(updatelabels) forcontrolevents:uicontroleventtouchupinside];
(just don't forget add iboutlet of button custom cell class)
Comments
Post a Comment