1

我看到了几个示例,但在尝试保存我的文本字段输入并在重新启动应用程序时重新加载它时,我无法弄清楚出了什么问题。我的 .m 文件中有类似的内容(.h 文件只有一个<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

我不确定到底出了什么问题,但是当我重新启动应用程序时,loadedText 总是:“¯8*:å”。不管我保存了什么。

4

2 回答 2

1

我发现最简单的解决方案是设置


ViewDidLoad

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

并在设置中

if (text!=nil) {
        textField.text=text;
    }
于 2013-12-19T13:06:06.757 回答
0

Setup 方法在哪里调用?我认为你在 viewDidLoad 中进行了初始化,但之前调用了 setup 方法。您不保存此信息,因此按钮不会识别或加载信息并在初始化之前设置文本。在设置方法中加载信息

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

看看会发生什么。

更新:

那是错误的;

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

你需要

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

请在您创建 c4WorkSpace 对象的位置发布代码。

更新 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"];
}

所以你这样称呼它:

C4WorkSpace *c4 = [C4WorkSpace alloc] init];
[c4 setup]
[c4 setTextView];
于 2013-12-19T12:33:15.147 回答