ios - Obj C - NSUserDefaults won't give back values to calculate, after loading the view -
i stuck little problem. have basic calculating app.
viewcontroller.m
#import "viewcontroller.h" @interface viewcontroller () <uitextfielddelegate, uialertviewdelegate> @end @implementation viewcontroller -(void)textfielddidendediting:(uitextfield *)textfield { self.currentsettings = _currentsettings; [self calculatetheprice]; } -(void)calculatetheprice { float wynik = self.currentsettings.kwh * self.currentsettings.price; self.pricelabel.text = [nsstring stringwithformat:@"%.02f %@", wynik , self.currentsettings.currency]; }
settingsvc.m
#import "settingsvc.h" @interface settingsvc () <uitextfielddelegate> @end @implementation settingsvc #pragma mark - userdefaults implementation -(void)viewwillappear:(bool)animated { [self createcurrencyarray]; nsuserdefaults *pricedef = [nsuserdefaults standarduserdefaults]; nsstring *pricedeftext = [pricedef stringforkey:@"pricecall"]; _pricetextfield.text = pricedeftext; nsuserdefaults *currencydef = [nsuserdefaults standarduserdefaults]; [_currencypicker selectrow:[currencydef integerforkey:@"currencycall"] incomponent:0 animated:no]; [pricedef synchronize]; [currencydef synchronize]; } -(void)viewwilldisappear:(bool)animated { nsstring *textofpricetexfield = _pricetextfield.text; [[nsuserdefaults standarduserdefaults] setobject:textofpricetexfield forkey:@"pricecall"]; }
now, problem when want program automatically-calculate, won't. have result, have switch second view, choose value picker , when go back, have result. but... - when change value on 1st screen, result won't change. when change value on 2nd scree, result won't change. when change value on pickerview - tadah - result updates!
- when go second view, , go first, go again second , go first, result changes "0.00 (null)"...
any ideas did wrong? think nsuserdefaults, tried many options, nothing worked, nor changed anything.
you need synchronize nsuserdefaults when set new values. using synchronize when retrieve values.
here don't need 2 pointers defaults , don't need synchronize:
nsuserdefaults *pricedef = [nsuserdefaults standarduserdefaults]; nsstring *pricedeftext = [pricedef stringforkey:@"pricecall"]; _pricetextfield.text = pricedeftext; [_currencypicker selectrow:[pricedef integerforkey:@"currencycall"] incomponent:0 animated:no];
here need synchronizes:
-(void)viewwilldisappear:(bool)animated { nsstring *textofpricetexfield = _pricetextfield.text; nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; [defaults setobject:textofpricetexfield forkey:@"pricecall"]; [defaults synchronize]; } -(void)pickerview:(uipickerview *)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component { nsinteger selectedrow = [_currencypicker selectedrowincomponent:0]; nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults]; [defaults setinteger:selectedrow forkey:@"currencycall"]; [defaults synchronize]; self.currentsettings.currency = [self.currencyarray objectatindex:row]; self.currentsettings.price = [self.pricetextfield.text floatvalue]; //[self userdidfinishsetting]; }
Comments
Post a Comment