In class 1 i save a int converted to a string, in class 2 I try to load this string for some reason it does not work, when I write the string which I get out of loadString to a text box in class 2 the text box stays empty.
When i do the same thing in class 1 the text box becomes “1”.
class 1.m
- (void)viewDidLoad
{
[super viewDidLoad];
countForString = 1;
saveString = [[NSString alloc]initWithFormat:@"%i", countForString];
[self SaveTextBox:saveString :@"Number"];
}
-(void)SaveTextBox:(NSString*)string :(NSString*)stringsave
{
NSString *savestring = string;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestring forKey:stringsave];
[defaults synchronize];
}
class 2.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadString:@"Number":teamString];
tbTeamPlayer.text = teamString;
}
-(void)loadString:(NSString *)location:(NSString *)saveInString
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstring = [defaults objectForKey:location];
saveInString = loadstring;
}
loadString:location:doesn’t change the value ofsaveInString, all it does is overwrite the local variable. You need to return the string and set that value, e.g.and change loadString to do: