I have a NSString that I use NSUserDefaults to store and move between multiple view controllers. The problem is, the NSString will not display in the label I am trying to show it in. Here is my code, I have no errors.
First View Controller:
NSString *scoreString = [NSString stringWithFormat:@"%d", score];
NSString *score = @"message";
[[NSUserDefaults standardUserDefaults]
setObject:scoreString forKey:@"score"];
Second view Controller:
- (void)viewDidLoad {
[[NSUserDefaults standardUserDefaults] objectForKey:@"Score"];
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKe开发者_C百科y:@"score"];
savedValue = score.text;
[super viewDidLoad];
}
I have no idea why the string is not getting displayed, the connections are all made. Any help would be great. Thanks!
savedValue = score.text;
Your assignment is backwards. You're (uselessly) overwriting the variable holding the string you fetched from NSUserDefaults with the current value of the text field.
I think what you want is
score.text = savedValue;
The following line is also not really doing anything:
[[NSUserDefaults standardUserDefaults] objectForKey:@"Score"];
I'm assuming in the second view controller, score is the name of your label?
If so, you should say
score.text = savedValue;
精彩评论