ios - Objecitive C - How to send SecondVC label text to FirstVC and update the FirstVC label text -
in case there 2 viewcontrollers
. in first view controller there label , when view load, it's text should display 1
. there button, when click,navigate second view controller. in second view , there stepper , label. if user tap +
,second view's label text change 1
9
,for -
same(decrease value). in second view there button.when click, second view dismiss (from first view second used presend modally
kind segue over current context
presentation.that means when dismiss secondview, firstview not load again,it exists in background). want send second view's label text (after changed stepper), first view's text , update first view's label.(think if second view's label text 3
, first view's label text should update 1
3
). tried nsuserdefaults
.this code.
this second view controller
- (void)viewdidload { [super viewdidload]; //set default value adult label nsuinteger defaultadultval = self.adultstepper.value; self.adultcountlabel.text = [nsstring stringwithformat:@"%d", defaultadultval]; } - (ibaction)adultcountaction:(uistepper *)sender { nsuinteger adultval = sender.value; self.adultcountlabel.text = [nsstring stringwithformat:@"%d", adultval]; nsstring *adultcount = [nsstring stringwithformat:@"%d", adultval]; [[nsuserdefaults standarduserdefaults] setobject:adultcount forkey:@"adultcount"]; [[nsuserdefaults standarduserdefaults] synchronize]; } - (ibaction)doneaction:(id)sender { [self dismissviewcontrolleranimated:yes completion:nil]; }
and second view controller
- (nsstring *)testingasign { nsstring *adltcount = [[nsuserdefaults standarduserdefaults] objectforkey:@"adultcount"]; return adltcount; }
i'm getting value method in first view, , want update first view's value didn't work.
there many ways are
- by using
protocol-delegate
- perfect way
proper , perfect way create protocol in secondvc, , add 1 weak property delegate, , while presenting secondvc.. assign firstvc delegate of secondvc. also, implement protocol in firstvc. when dismissing secondvc, call method in protocol. , implemented method in firstvc called.. value there.
- by using
nsnotification
you can add observer notification in firstvc , postnotification secondvc. not proper way.. firstvc continuously observes notification. (don't forget remove observer.. once dont require observation)
- by using
global variable
you can add 1 global variable in appdelegate, , assign value secondvc. , access value firstvc. not proper way. because variable remain in memory.
Comments
Post a Comment