1

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.

suMi
  • 1,536
  • 1
  • 17
  • 30

2 Answers2

1

I found the easiest solution is to set in


ViewDidLoad

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

and in setup

if (text!=nil) {
        textField.text=text;
    }
suMi
  • 1,536
  • 1
  • 17
  • 30
0

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];
gronzzz
  • 617
  • 6
  • 15
  • could you explain this a bit more? How would that comment help me? – suMi Dec 19 '13 at 12:35
  • viewDidLoad is called before the setup. – suMi Dec 19 '13 at 12:37
  • and that's wrong, you call viewDidLoad, and there set variable to undefined uitextview, so text will be not right. You need call viewdidload without loading from default – gronzzz Dec 19 '13 at 12:44
  • so what is the solution then? setting up the textfield in viewDidLoad? – suMi Dec 19 '13 at 12:58
  • check out my answer, i updated it twice. for example creating textfield in viewDidLoad, or set .text parameter in setup method. – gronzzz Dec 19 '13 at 13:01
  • thanks. through you I found what's going wrong!! I posted another answer to this. To me that's the easiest solution.. – suMi Dec 19 '13 at 13:09
  • you anyway need this to make in setup method or after, and your solution is strange, because if defaults nor found, then it will return nil and nil will set to text, so text will be @"", you needn't to check if string != nil – gronzzz Dec 19 '13 at 13:13
  • Also in SO always if answer is helpful, then sets checkmark and up vote him. – gronzzz Dec 19 '13 at 13:16