0

I need to share a string between two views in my application. When the user ends the game, the score is converted into a string. I then need to transfer that string into a different view controller where I display the score. I have a label set up and all but the view is not recognizing the string even though I am importing the header file from which the string is created. Any help would be great, thank.

This is my view controller where the string is created

NSString *scoreString = [NSString stringWithFormat:@"%d", score];

And this is where I try to display the string in a different view controller

- (void)viewDidLoad {
    self.scoreString = score.text;

    [super viewDidLoad];
}
Yo_Its_Az
  • 2,043
  • 2
  • 18
  • 25

2 Answers2

5

For scores and similar data, you may want to use NSUserDefaults. These can be accessed at any time from any UIViewController. For example, you can implement methods similar to these to save and retrieve the data:

-(void)saveToUserDefaults:(NSString*)myString
{
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

    if (standardUserDefaults) {
        [standardUserDefaults setObject:myString forKey:@"Score"];
        [standardUserDefaults synchronize];
    }
}

-(NSString*)retrieveFromUserDefaults
{
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    NSString *val = nil;

    if (standardUserDefaults) 
        val = [standardUserDefaults objectForKey:@"Score"];

    return val;
}

NSUserDefaults also handles ints, BOOLs, NSArrays, etc. Check out the documentation or google around for examples.

PengOne
  • 48,188
  • 17
  • 130
  • 149
  • How do I move the string into NSUserDefaults? Thanks. – Yo_Its_Az Jun 27 '11 at 18:30
  • Ok, the NSUser Default is working great but now how do I set the score.text to equal the objectForKey:@"Score"? – Yo_Its_Az Jun 27 '11 at 18:43
  • @MacN00b Just put `score.text` where my code reads `myString`. – PengOne Jun 27 '11 at 18:47
  • Ok this is what I did, [[NSUserDefaults standardUserDefaults] setObject:@"message" forKey:@"Score"]; Then I retrive it by doing this [[NSUserDefaults standardUserDefaults] objectForKey:@"Score"]; But now I am trying to put that value into the text box and by doing self.message = score.text; but I get the error Property 'message' not found on object of type 'ViewController'. Any idea why? – Yo_Its_Az Jun 27 '11 at 19:16
  • 1
    @MacN00b: there is no `self.message` unless you have declared and synthesized an `NSString` called `message`. Try using the methods I defined above and calling `[self saveToUserDefaults:score.text];` and then `score.text = [self retrieveFromUserDefaults];`. – PengOne Jun 27 '11 at 20:58
2

You could use delegates or NSUserDefaults

Been asked similiar questions before:

Passing variables to different view controllers

How do I pass variables between view controllers?

EDIT

[[NSUserDefaults standardUserDefaults] setObject:@"STRING HERE" forKey:@"MyKey"];

and retrieve

[[NSUserDefaults standardUserDefaults] objectForKey:@"MyKey"];
Community
  • 1
  • 1
LuckyLuke
  • 47,771
  • 85
  • 270
  • 434