xcode NSUserDefaults没有保存

时间:2014-08-06 14:52:14

标签: xcode nsuserdefaults

1.无论我做什么,saveButton方法都不会保存,但仍然会为REJECTIONS返回0 2.请帮我修复我的saveButton到userdefaults方法 3.我尝试了多种方法,价值仍然没有改变

#import "CCViewController.h"

@interface CCViewController ()

@end

@implementation CCViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    [NSUserDefaults integerForKey:@"REJECTIONS"];

    self.rejectLabel.text=REJECTIONS;
    self.acceptedLabel.text=ACCEPTED;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.customInputTextField resignFirstResponder];
}
- (IBAction)resetButton:(UIButton *)sender {
    self.rejectLabel.text=@"0";
    self.acceptedLabel.text=@"0";

}
- (IBAction)rejectButton:(UIButton *)sender {
    int current=stringToInt(self.rejectLabel.text);
    int test=stringToInt(self.customInputTextField.text);
    if (test>0) {
        self.rejectLabel.text=self.customInputTextField.text;
        self.customInputTextField.text=nil;
    } else {
        current++;
        self.rejectLabel.text=intToString(current);
    }
    if (stringToInt(self.rejectLabel.text)==3000) {
        UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"Achievement Message" message:@"You have reached 3000 rejections!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: nil];
        [alertView show];
    }
}

NSInteger stringToInt(NSString *string) {
    return [string integerValue];
}

NSString* intToString(NSInteger integer) {
    return [NSString stringWithFormat:@"%d", integer];
}

- (IBAction)acceptedButton:(UIButton *)sender {
    int current=stringToInt(self.acceptedLabel.text);
    int test=stringToInt(self.customInputTextField.text);
    if (test>0) {        
        self.acceptedLabel.text=self.customInputTextField.text;
        self.customInputTextField.text=nil;
    } else {
        current++;
        self.acceptedLabel.text=intToString(current);
    }
    NSLog(@"after is: %i", current);
}

- (IBAction)unrejectButton:(UIButton *)sender {
    int current=stringToInt(self.rejectLabel.text);
        current--;
    if (current>=0) {
        self.rejectLabel.text=intToString(current);
    } else {UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"Error Message" message:@"You can not have negative rejections!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alertView show];}
    NSLog(@"after is: %i", current);
}
- (IBAction)saveButton:(UIButton *)sender {
    NSString *rejected=self.rejectLabel.text;
    int reject=intToString(self.rejectLabel.text);
    NSString *accepted=self.acceptedLabel.text;
    NSUserDefaults *rejectTry=[NSUserDefaults standardUserDefaults];
    [rejectTry setObject:self.rejectLabel.text forKey:REJECTIONS];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:self.rejectLabel.text forKey:REJECTIONS];
    [[NSUserDefaults standardUserDefaults] setValue:@"reject" forKey:REJECTIONS];
    [[NSUserDefaults standardUserDefaults] setValue:self.rejectLabel.text forKey:REJECTIONS];
    [[NSUserDefaults standardUserDefaults] setObject:@"accepted" forKey:ACCEPTED];
}
    NSLog(@"test save %@ & %@ & %@ & %@", REJECTIONS, ACCEPTED, self.rejectLabel.text, self.acceptedLabel.text);
    [rejectTry synchronize];
    NSLog(@"test save %@ & %@ & %@ & %@, &a %i", REJECTIONS, ACCEPTED, self.rejectLabel.text, self.acceptedLabel.text, picker_value);

}

- (IBAction)loadButton:(id)sender {
    self.rejectLabel.text=REJECTIONS;
    self.acceptedLabel.text=ACCEPTED;

}
@end
  1. 仍未保存到拒绝密钥并已接受

    NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults]; [prefs setObject:self.rejectLabel.text forKey:REJECTIONS];

    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:self.acceptedLabel.text forKey:ACCEPTED];

    [[NSUserDefaults standardUserDefaults] synchronize]; NSLog(@" test save%@&%@&%@&%@ pointers%@%@",REJECTIONS,ACCEPTED,self.rejectLabel.text,self.acceptedLabel.text,defaults ,prefs);

1 个答案:

答案 0 :(得分:0)

看起来你已经弄乱了你的指针。您有两个对同一个userDefaults实例的引用,但是当您写入每个引用时,您会得到不同的结果。在这里,让我们来看看你的代码

NSString *rejected=self.rejectLabel.text;
int reject=intToString(self.rejectLabel.text);
NSString *accepted=self.acceptedLabel.text;
NSUserDefaults *rejectTry=[NSUserDefaults standardUserDefaults];

让我们到此为止。 standardUserDefaults是一个单例对象。调用此方法时,您不是在创建用户默认对象。然后,您继续设置值

[rejectTry setObject:self.rejectLabel.text forKey:REJECTIONS];

这很好用。

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:self.rejectLabel.text forKey:REJECTIONS];

现在你已经写了以前的价值!

[[NSUserDefaults standardUserDefaults] setValue:@"reject" forKey:REJECTIONS];

你再来一次!

[[NSUserDefaults standardUserDefaults] setValue:self.rejectLabel.text forKey:REJECTIONS];
[[NSUserDefaults standardUserDefaults] setObject:@"accepted" forKey:ACCEPTED];

<强>结论

您应该做的是创建一个像NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];这样的局部变量然后将其用于所有写入。然后,您将看到如何多次使用键REJECTIONS覆盖该值