saving user defaults [iOS]

4.9k views Asked by At

I saw a couple of examples but I cannot figure out what's going wrong when trying to save my text field input and reload it when restarting the app. I have something like this in my .m file (.h file only has a <UITextViewDelegate>);

@implementation C4WorkSpace{
    UITextView *textField;
    C4Button *okButton;
    C4Label *savedText;
}

-(void)setup {
    //add text field
    CGRect textViewFrame = CGRectMake(20.0f, 20, self.canvas.width-40, 124.0f);
    textField = [[UITextView alloc] initWithFrame:textViewFrame];
    textField.returnKeyType = UIReturnKeyDone;
    [textField becomeFirstResponder];
    textField.delegate = self;
    //textField.hidden=true;

    [self.view addSubview:textField];

    okButton=[C4Button buttonWithType:ROUNDEDRECT];
    [okButton setTitle:@"Save" forState:NORMAL];
    okButton.center=self.canvas.center;
    [self.canvas addUIElement:okButton];
    [okButton runMethod:@"saveDefault" target:self forEvent:TOUCHUPINSIDE];

    savedText=[C4Label labelWithText:@"default"];
    savedText.center=CGPointMake(self.canvas.center.x, self.canvas.center.y+40);
    [self.canvas addLabel:savedText];
}
-(void)saveDefault{
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];

    [defaults setObject:textField.text forKey:@"userName"];
    [defaults synchronize];
    C4Log(@"defaults: %@",defaults);
    C4Log(@"defaults: %@", [defaults objectForKey:@"userName"]);
    savedText.text=textField.text;
}
-(void)viewDidLoad{
    NSMutableString *text=[[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    C4Log(@"loadedText:%s", text);
    textField.text=text;
    savedText.text=text;
}
@end

I'm not sure what exactly is going wrong, but when I restart the app the loadedText is always: "¯8*:å". Doesn't matter what I saved.

2

There are 2 answers

0
suMi On BEST ANSWER

I found the easiest solution is to set in


ViewDidLoad

text=[[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];

and in setup

if (text!=nil) {
        textField.text=text;
    }
8
gronzzz On

Where does Setup method is call ? I think in viewDidLoad you initialize, but setup method called earlier. You don't save this info,so button don't recognizer or load info and set text before initialization. in setup method load info

UItextView *k = [UItextView alloc] init];
k.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];

and take a look what will be.

UPDATE:

That's wrong;

[c4workspace loadInfoFromDefaults];
[c4workspace alloc]init];  (calls viedidload method with load info)

You need

[c4workspace alloc]init]; 
[c4workspace loadInfoFromDefaults];

Please post code where you create c4WorkSpace object.

UPDATE 2:

-(void)setup {

    //add text field
    CGRect textViewFrame = CGRectMake(20.0f, 20, self.canvas.width-40, 124.0f);
    textField = [[UITextView alloc] initWithFrame:textViewFrame];
    textField.returnKeyType = UIReturnKeyDone;
    [textField becomeFirstResponder];
    textField.delegate = self;
    //textField.hidden=true;

    [self.view addSubview:textField];

    okButton=[C4Button buttonWithType:ROUNDEDRECT];
    [okButton setTitle:@"Save" forState:NORMAL];
    okButton.center=self.canvas.center;
    [self.canvas addUIElement:okButton];
    [okButton runMethod:@"saveDefault" target:self forEvent:TOUCHUPINSIDE];

    savedText=[C4Label labelWithText:@"default"];
    savedText.center=CGPointMake(self.canvas.center.x, self.canvas.center.y+40);
    [self.canvas addLabel:savedText];
}

- (void) setTextView
{
    textField.text=[[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    savedText.text=[[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
}

So you call this like:

C4WorkSpace *c4 = [C4WorkSpace alloc] init];
[c4 setup]
[c4 setTextView];