Eksperiment626
Eksperiment626

Reputation: 1007

Xcode: Why doesn't my app save the UITextField?

I have an app where I switch to a view with a TableView using presentModalViewController. The View with the TableView is however not at UITableViewController but a regular UIViewController. I have written UITextFields into the UITableViewCells. I use this action to save what the user inputs:

- (IBAction)saveTextField:(id)sender {

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:TextField.text forKey:@"keyTextField"];
[userDefaults synchronize]; }

I call this method with this code:

    [TextField addTarget:self action:@selector(hideKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];

When I dismiss the ViewController using dismissModalViewController and then when I presents it again I call the NSUserDefaults in the ViewDidLoad with this code:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
TextField.text = [userDefaults objectForKey:@"keyTextField"];

Problem is that it doesn't work. And I can't figure out the problem. Any help would be appreciated.

Thanks in advance :)

Upvotes: 0

Views: 496

Answers (4)

Eksperiment626
Eksperiment626

Reputation: 1007

To begin with: thanks to all of you for your effort and time :)

Turns out the problem wasn't with the code that saves and loads it. The problem was that I only created one UITextfield which I then put into every UITableViewCell and that was the problem. So I created individual UITextFields for every UITableViewCells and that fixed the problem :)

Upvotes: 1

Arun
Arun

Reputation: 2281

use this code in view did load NSUserDefaults *user=[NSUserDefaults standardUserDefaults]; TextField.text=[user stringForKey:@"keyTextField"];

Upvotes: 1

ott--
ott--

Reputation: 5722

Instead of setting a target, can't you do that saving with the textFieldDidEndEditing: delegate method?

Upvotes: 1

JonasG
JonasG

Reputation: 9324

The target has to be the same as your method, meaning that if you have - (void)saveTextField:(id)sender then you code to add the target need to be

 [TextField addTarget:self action:@selector(saveTextField:) forControlEvents:UIControlEventEditingDidEndOnExit];

Check usin NSLog if your action is being run.

Upvotes: 1

Related Questions