ios - How to make one label to set score on all view controllers? -


i have label , need show score, coins ect on every viewcontroller, means when score changes changes every throughout whole app...

i've tried set label show score on whole app cant figure out how!

please help

this have far in view controller:

 -(void)viewdidload { { [super viewdidload];       nserror *error;     nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); //1     nsstring *documentsdirectory = [paths objectatindex:0]; //2     path = [documentsdirectory stringbyappendingpathcomponent:@"settingslist.plist"]; //3      nsfilemanager *filemanager = [nsfilemanager defaultmanager];      if (![filemanager fileexistsatpath: path]) //4     {         nsstring *bundle = [[nsbundle mainbundle] pathforresource:@"settingslist"oftype:@"plist"]; //5 //5          [filemanager copyitematpath:bundle topath: path error:&error]; //6     }      savedstock = [[nsmutabledictionary alloc] initwithcontentsoffile: path];      npoint = [[savedstock objectforkey:@"point"] intvalue];     [giftamount settext:[nsstring stringwithformat:@"%d",npoint]];  [self updatecurrencybalance]; [self zoneloading]; }  //adcolony - (void) viewdidappear:(bool)animated { [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(updatecurrencybalance) name:kcurrencybalancechange object:nil];  [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(zoneready) name:kzoneready object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(zoneoff) name:kzoneoff object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(zoneloading) name:kzoneloading object:nil]; }  // currency balance persistent storage , display - (void)updatecurrencybalance { nsnumber* wrappedbalance = [[nsuserdefaults standarduserdefaults] objectforkey:kcurrencybalance]; nsuinteger balance = wrappedbalance && [wrappedbalance iskindofclass:[nsnumber class]] ? [wrappedbalance unsignedintvalue] : 0; [giftamount settext:[nsstring stringwithformat:@"%u", balance]];  [savedstock setobject:[nsnumber numberwithfloat:npoint = balance] forkey:@"point"]; [savedstock writetofile: path atomically:yes];  } 

i have action in other playviewcontroller (minusus) -200 coins, not updating in viewcontroller?

one way use nsnotificationcenter.

add code places change value of score:

- (void)updatescore:(nsnumber *)newvalue     // update score     self.score = newvalue;      // create dictionary object containing score sent notification     nsmutabledictionary* userinfo = [nsmutabledictionary dictionary];     [userinfo setobject:self.score forkey:@"score"];      // add send notification listeners in whole app     [[nsnotificationcenter defaultcenter] postnotificationname:@"notificationscorechanged" object:nil userinfo:userinfo]; } 

in viewdidload methods of view controllers, add code:

- (void)viewdidload:(bool)animated {     [super viewdidload:animated];      // add code start receiving notifications (become listener)     [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(scorechanged:) name:@"notificationscorechanged" object:nil]; } 

then somewhere in view controllers, add method update ui:

- (void)scorechanged:(nsnotification *)notification {    // retrieve score results     if ([notification.name isequaltostring:@"notificationscorechanged"])     {         nsdictionary *userinfo = notification.userinfo;         nsnumber *score = [userinfo objectforkey:@"score"];          // , update labels         self.scorelabel.text = [score description];     } 

and in view controllers, add dealloc:

- (void)dealloc {     //unregister (stop listening)     [[nsnotificationcenter defaultcenter] removeobserver:self]; } 

note: should adapt code depending on how store , retrieve score. ie, if use nsuserdefaults (see @erid's answer), coredate, etc.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -