objective c - iOS - Pass data from one viewcontroller to use in the init method of another viewcontroller -


i'm able pass data between 2 view controllers using prepareforsegue method. in way, passed data cannot used in second view controller's init method.

also, i'm using xlform. accessing data inside init method necessary.

can me solve problem.

here's first view controller's prepareforsegue method:

- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender {      if ([[segue identifier] isequaltostring:@"sessiontoworkout"])     {         workoutsviewcontroller *vc = [segue destinationviewcontroller];          nsindexpath *indexpath = [self.tableview indexpathforselectedrow];          uitableviewcell *selectedcell = [self.tableview cellforrowatindexpath:indexpath];         cellname = selectedcell.textlabel.text;          nsstring *sectiontitle = [self tableview:self.tableview titleforheaderinsection:indexpath.section];         sectionname = sectiontitle;          vc.sectionname = sectionname;         vc.cellname = cellname;     }  } 

and here's initwithcoder method of second view controller:

- (instancetype)initwithcoder:(nscoder *)coder      {         self = [super initwithcoder:coder];         if (self) {             //retrieve workouts db             nsstring *day_id;              day_id = [[dbhandler database] getdayidwhere:@[@"day_name"]                                              wherevalues:@[cellname]];              workoutsarray = [[dbhandler database] getworkoutsfordaywhere:@[@"day_id"]                                                              wherevalues:@[day_id]];              applog(@"workouts array %@", workoutsarray);              [self initializeform];         }         return self;     } 

inside initwithcoder method, need use cellname variable's value (which has been passed view controller previous view controller) call database method.

any idea or suggestions how that? in advance.

the didset observer called when variable modified (it's not called when variable initialized). initialize database when cellname set :

swift:

var cellname : string = "" {     didset {      // cell name has been set, create database here in other function     } } 

objective-c:

it's quite similar in objective c, don't have didset observer, instead use custom setter. difference is, since it's setter, have set variable

@property(nonatomic, strong) nsstring * cellname;  -(void)setcellname:(nsstring *)newvalue {     // first, set new value variable     _cellname = newvalue;      // cell name has been set, create database here  } 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -