如何使用NSUserDefaults正确保存字段?

时间:2015-05-06 00:38:48

标签: objective-c nsuserdefaults

我正在创建一个程序,用于从一个文本字段保存字符串,从第二个文本字段保存一个整数。我正在尝试使用NSUserDefaults,但是当我运行程序时,我似乎无法正确地保存它们。当我在处理NSUserDefaults时按下带有断点的保存按钮时,它会崩溃。难道我做错了什么?我浏览了文档,但没有帮助。我甚至可以使用两个不同的NSUserDefaults实例吗?

- (IBAction)save:(id)sender {
    //Save stuff fropm textfields
    NSString* name = _nameText.text;
    int count = [_countText.text intValue];

    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"name"];
    [[NSUserDefaults standardUserDefaults] setInteger:count forKey:@"count"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


- (IBAction)load:(id)sender {
    //Load stuff from NSUserDefaults
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    NSString* loadstring = [defaults objectForKey:@"name"];
    _nameText.text =  loadstring;

    NSUserDefaults* defaults2 = [NSUserDefaults standardUserDefaults];
    NSInteger loadint = [defaults2 integerForKey:@"count"];
    _countText.text = [NSString stringWithFormat:@"%li", (long)loadint];
}

1 个答案:

答案 0 :(得分:1)

//tooltip code within an if statement; does not work .on("mouseover", function(d) { if(svg.dot.style("opacity", 0)==false){ // << IS THIS RIGHT? div.transition() .duration(200) .style("opacity", .8); div .html(d.datetime.substring(0,10) ) .style("left", (d3.event.pageX + 5) + "px") .style("top", (d3.event.pageY - 24) + "px"); } }) .on("mouseout", function(d) { div.transition() .duration(500) .style("opacity", 0); }) (以及.h中的defaults = [NSUserDefaults standardDefaults];)中声明viewDidLoad并使用此代码:

NSUserDefaults* defaults;