objective c - How to populate an array from detailViewController and return to masterViewController in iOS? -
i have tableview
navigate 'viewcontroller' ie passengerinfoviewcontroller
in have form.i fill form , make dictionary
out of , add nsmutablearray
.
so when going tableview' pass array , reload the
tableview` filled array.
so here's doing : -
//navigate form - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { passengerinfoviewcontroller *pivc = [self.storyboard instantiateviewcontrollerwithidentifier:@"passengerinfoviewcontroller"]; [self.navigationcontroller pushviewcontroller:pivc animated:yes]; } //after filling form -(void)gobacktopassengerdetails:(uibutton*)sender{ nsstring *title = self.titletextfield.text; nsstring *fname = self.firstnametextfield.text; nsstring *lname =self.lastnametextfield.text; nsstring *agestr = self.agetextfield.text; nsmutabledictionary *dict = [[nsmutabledictionary alloc]init]; [dict setvalue:title forkey:@"title"]; [dict setvalue:fname forkey:@"firstname"]; [dict setvalue:lname forkey:@"lastname"]; [dict setvalue:agestr forkey:@"age"]; passenger *passengerobj = [passenger sharedinstance]; //singleton [passengerobj.passengerdetailarray addobject:dict]; passengerdetailsviewcontroller *pdc = [self.storyboard instantiateviewcontrollerwithidentifier:@"passengerdetailsviewcontroller"]; [pdc getpassengerinfo:passengerobj.passengerdetailarray]; [self.navigationcontroller popviewcontrolleranimated:yes];
once navigate reload table view.however celforrow
method doesn't populate new values.
-(void)getpassengerinfo:(nsmutablearray*)arr{ passenger_infoarray =[[nsmutablearray alloc]init]; passenger_infoarray = arr; nslog(@"passenger info array : %@", passenger_infoarray);//shows array of dictionaries [passengerinfotableview reloaddata]; }
my cellforrow
method looks this:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { //not called when doing reload data static nsstring *cellidentifier = @"cell"; cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstylevalue1 reuseidentifier:cellidentifier ]; } nslog(@"table view array : %@",passenger_infoarray); if (passenger_infoarray.count>0) { nsstring *temp= [[passenger_infoarray objectatindex:indexpath.row]objectforkey:@"firstname"]; cell.textlabel.text = temp; }else{ cell.textlabel.text = [nsstring stringwithformat:@"passenger %ld", (long)indexpath.row+1]; cell.imageview.image = [uiimage imagenamed:@"user.png"]; } return cell; }
you can pass data using
- delegates
- notifications
- properties
- global variables
using delegates practice, , last 2 not preferred.
Comments
Post a Comment