Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9241463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:22:35+00:00 2026-06-18T08:22:35+00:00

The problem I get when I use the codes below is that the totalScore

  • 0

The problem I get when I use the codes below is that the totalScore only updates when the viewDidLoad method in the secondViewController is called. So if I press the addOne button twice in a row the retrievingString is the same both times so it will just add one to the same existing value, because it haven’t been updated in the secondViewController. How can I manage to update the totalScore in secondViewController each time the addOne button is pressed in firstViewController

firstViewController:


-(void)saveString:(NSString *)myString {
[[NSUserDefaults standardUserDefaults] setObject:myString forKey:@"firstScore"];
}

-(NSString *)retrieveString {
NSString *recoveredString = [[NSUserDefaults standardUserDefaults]objectForKey:@"totalScore"];
return recoveredString;
}

-(IBAction) addOne {
    NSString *totalScore = [self retrieveString];
    NSInteger totalScoreInt = [totalScore intValue];

    NSInteger newScore = totalScoreInt+1;
    NSString *newScoreString = [NSString stringWithFormat:@"%i", newScore];
    [self saveString:newScoreString];

}


secondViewController:


-(void)saveString:(NSString *)myString {
[[NSUserDefaults standardUserDefaults] setObject:myString forKey:@"totalScore"];
}

-(NSString *)retrieveString {
NSString *recoveredString = [[NSUserDefaults     standardUserDefaults]objectForKey:@"firstScore"];
return recoveredString;
}

-(void) viewDidLoad {
    NSString *firstScore = [self retrieveString];
    NSInteger firstScoreInt = [firstScore intValue];

    NSInteger newTotalScore = firstScoreInt+1;
    NSString *newTotalScoreString = [NSString stringWithFormat:@"%i", newTotalScore];
    [self saveString:newTotalScoreString];
 }

I hope I have explained the problem understandable, so maybe someone can help me with this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:22:36+00:00Added an answer on June 18, 2026 at 8:22 am

    If I’m understanding this correctly, the error that u’r making here is that since addOne function in your firstViewController is also updating the total score, but you are not saving the total score there. So in your saveString function in the firstViewController, add this line too :

    [[NSUserDefaults standardUserDefaults] setObject:myString forKey:@"totalScore"];
    

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that I can't get through. I just started to use
The problem is that when I use source code below, i receive cell id's
I mean that I use below code to get html source from url. But
I get this problem while doing some experiment with set . I use a
So I am running into this problem. I am trying to use $.get() to
I need to use something like get_or_create() but the problem is that I have
I'm trying to use prototype inheritance in Javascript. The problem with my code below
I have a a databasehelper and an activity with codes given below. The problem
I've run into a rather interesting problem. The code below generates an array that,
What's wrong with the code below ? When run, I get: Use of uninitialized

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.