Reputation: 253
I have an uitext field iboutlet collection and i want to load the data, but loadData doesn't work, only saveData.
saveData:
-(IBAction)saveData:(id)sender{
for (UITextField*longitud in guardarDatos){
NSString*saveString=longitud.text;
NSUserDefaults*defauls = [NSUserDefaults standardUserDefaults];
[defauls setObject:saveString forKey:@"savedString"];
[defauls synchronize];
NSLog(@"%@",saveString);
}
}
loadData:
-(IBAction)loadData:(id)sender{
for (UITextField*longitud in guardarDatos){
NSUserDefaults*defauls = [NSUserDefaults standardUserDefaults];
NSString*loadString=[defauls objectForKey:@"savedString"];
[longitud setText:loadString];
NSLog(@"%@",longitud);
}
}
Upvotes: 0
Views: 192
Reputation: 39978
You are saving different objects with same key. So use different keys for different objects
saveData
-(IBAction)saveData:(id)sender{
UITextField*longitud = nil;
for (int i = 0; i < guardarDatos.count; i++){
longitud = [guardarDatos objectAtIndex:i];
NSString*saveString=longitud.text;
NSUserDefaults*defauls = [NSUserDefaults standardUserDefaults];
[defauls setObject:saveString forKey:[NSString stringWithFormat:@"savedString%d",i]];
[defauls synchronize];
NSLog(@"%@",saveString);
}
}
loadData:
-(IBAction)loadData:(id)sender{
UITextField*longitud = nil;
for (int i = 0; i < guardarDatos.count; i++){
longitud = [guardarDatos objectAtIndex:i];
NSUserDefaults*defauls = [NSUserDefaults standardUserDefaults];
NSString*loadString=[defauls objectForKey:[NSString stringWithFormat:@"savedString%d",i]];
[longitud setText:loadString];
NSLog(@"%@",longitud);
}
}
Upvotes: 1
Reputation: 29064
Even though you are saving correctly, at the end of saveData, objectForKey:@"savedString" has only one value which is the last value you saved. When you load the data, it will load the same data for each of the textfield. Your logic seems to be wrong. Set a different name for the savedString everytime and try to obtain the value from the new name.
Another suggestion, try running saveData and loadData for one Textfield, before running for all the textfields that you have..
Upvotes: 0