ios - Pass an object defined from a customed tableViewCell -
to explain situation, have tableview custom tableviewcell , have created button in customed cell. want button add object box defined nsmutablearray , pass array restocardconfirmationviewcontroller in order create cart.
the problem when try display nsmutablearray in restocardconfirmationviewcontroller, result nil. code :
the boxtableviewcell :
#import "boxtableviewcell.h" #import "restocardconfirmationviewcontroller.h" #import "restaucardviewcontroller.h" @implementation boxtableviewcell { nsmutablearray *_pickerplace; } - (ibaction)select:(id)sender { restocardconfirmationviewcontroller *restaucardconfirmation = [[restocardconfirmationviewcontroller alloc] init]; [restaucardconfirmation.boxescommande addobject:_box]; nslog(@"la box choisie est %@",_box); self.select.enabled = no; if(!self.select.enabled){ nslog(@"button desabled"); } } the restocardconfirmationviewcontroller.h :
#import <uikit/uikit.h> #import <parse/parse.h> @interface restocardconfirmationviewcontroller : uiviewcontroller @property(nonatomic) nsmutablearray *boxescommande; the restocardconfirmationviewcontroller.m :
#import "restocardconfirmationviewcontroller.h" #import "choisirdateviewcontroller.h" @interface restocardconfirmationviewcontroller () @end @implementation restocardconfirmationviewcontroller - (void)viewdidload { [super viewdidload]; nslog(@"the nsmutablearray in other %@",self.boxescommande); // additional setup after loading view. }
one approach delegation. create delegate notifies every object added restocardconfirmationviewcontroller.
and other simpler method :
-(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [self.purchases count]; } in mainviewviewcontroller, add button click event instead of using in tableview cell.
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { cell.button.tag = indexpath.row; [cell.button addtarget:self action:@selector(select) forcontrolevents:uicontroleventtouchupinside]; // ... } this calls following method, implemented in mainviewcontroller
- (ibaction)select:(id)sender { int index = ((uibutton *)sender).tag;//it returns indexpath [self.boxesarray addobject: self.purchases[index]]; nslog(@"la box choisie est %@",_box); self.select.enabled = no; if(!self.select.enabled){ nslog(@"button desabled"); } } now in prepareforseque, pass created array restocardconfirmationviewcontroller.
- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { // make sure segue name in storyboard same line if ([[segue identifier] isequaltostring:@"your_segue_name_here"]) { // reference destination view controller restocardconfirmationviewcontroller *vc = [segue destinationviewcontroller]; // pass objects view controller here, like... vc.boxescommande = self.boxesarray; } }
Comments
Post a Comment