Finn Fuller
Finn Fuller

Reputation: 154

Tags and saving data with TextField

In my .xib file I have a button that when pressed generates 3 UITextFields. These text fields each have a tag so they are declared independently. My problem is that I want to save these text fields using NSUserDefaults and I'm not sure how to do that. I have been able to write out everything using no loops but if I could do it with loops and tags I would be very happy. I'm just not sure if I can use tags or really how to do it at all.

I've been able to create the text fields with an if statement(to make a limit on how many can be created) when the button is pressed but beyond that I am not sure. Here is what I have so far...

int y = 115;  // For height of buttons in addClass
int i = 1;   // Counter

- (IBAction)addClass:(id)sender
{
    if (i < 8) {


    UITextField *class = [[UITextField alloc] initWithFrame:CGRectMake(20, y, 118, 30)];
        class.tag = i;
class.placeholder = @"Class";
class.borderStyle = UITextBorderStyleRoundedRect;
class.font = [UIFont fontWithName:@"System" size:14.0];
class.textAlignment = NSTextAlignmentCenter;

[self.view addSubview:class];

UITextField *grade = [[UITextField alloc] initWithFrame:CGRectMake(169, y, 59, 30)];
    grade.tag = i;
grade.placeholder = @"Grade";
grade.borderStyle = UITextBorderStyleRoundedRect;
grade.font = [UIFont fontWithName:@"System" size:14.0];
grade.textAlignment = NSTextAlignmentCenter;

[self.view addSubview:grade];

UITextField *credit = [[UITextField alloc] initWithFrame:CGRectMake(236, y, 64, 30)];
    credit.tag = i;
credit.placeholder = @"Credits";
credit.borderStyle = UITextBorderStyleRoundedRect;
credit.font = [UIFont fontWithName:@"System" size:14.0];
credit.textAlignment = NSTextAlignmentCenter;

[self.view addSubview:credit];

y = y + 45;
i++;
}}



- (IBAction)save:(id)sender
{

//Grade1 save string
NSString *saveString0 = grade1.text;
NSUserDefaults *defaults0 = [NSUserDefaults standardUserDefaults];
[defaults0 setObject:saveString0 forKey:@"saveString0"];
[defaults0 synchronize];

//Grade2 save string
NSString *saveString1 = grade2.text;
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
[defaults1 setObject:saveString1 forKey:@"saveString1"];
[defaults1 synchronize];

//Grade3 save string
NSString *saveString2 = grade3.text;
NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
[defaults2 setObject:saveString2 forKey:@"saveString2"];
[defaults2 synchronize];
...
}

As you can see save string for the grades are all individually coded. What I would like to learn how to do is create a for loop and the expression will go up to the value of i(the counter) so something like...

for(int j = 0; j <= i; j++)

and then j is the value that would go into the *saveString, *defaults, and grade.text. If anyone knows how to do this or has a better solution I would be very grateful.

Thanks pedros. One last thing, if I have...

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadString = [defaults objectForKey:@"saveString"];
[grade1 setText:loadString];
[loaded setText:@"Data Loaded Successfully."];

How would I change that to implement the for loop as well?

Upvotes: 0

Views: 184

Answers (1)

pedros
pedros

Reputation: 1197

Try something like this.

NSArray *grades = @[grade1, grade2, grade3, ...];

for(int j = 0; j <= i; j++) {
    NSString *saveString = [(UITextField*)grades[j] text];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *key = [NSString stringWithFormat:@"saveString%d", j];
    [defaults setObject:saveString forKey:key];
}
[defaults synchronize];

Second part would be really similar.

NSArray *grades = @[grade1, grade2, grade3, ...];

for(int j = 0; j <= i; j++) {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *loadString = [defaults objectForKey:@"saveString%d", j];
    [(UITextField*)grades[j] setText:loadString];
}
[loaded setText:@"Data Loaded Successfully."];

Upvotes: 1

Related Questions