在本地存储复选框值并再次返回

时间:2015-06-02 06:05:42

标签: ios objective-c

你好,我有问题列表哪个用户只能在YES或NO给出答案,所以我有自定义复选框,图像checkbox_fill和空复选框。当有人选择YES,No将为空复选框,如果选择NO,YES将为空不会填补。现在的问题是我想在本地存储这个选中的按钮,如果是或否。如果用户从一个屏幕移动到另一个屏幕,则返回。我的代码如下,选择是或否。

- (IBAction)btnCheckBoxfor1:(id)sender {
    UIImage *selected = [UIImage imageNamed:@"checkbox_fill.png"];
    UIImage *notSelected = [UIImage imageNamed:@"checkbox.png"];

    if ([sender tag]==1) { // checkbox yes
        if (![sender isSelected]) {
            [sender setSelected:YES];
            [self.btnNO setSelected:NO];
            [self.btnNO setImage:notSelected forState:UIControlStateNormal];
            [self.btnYES setImage:selected forState:UIControlStateNormal];
        }
    }else{ // checkbox no
        if (![sender isSelected]) {
            [sender setSelected:YES];
            [self.btnYES setSelected:NO];
            [self.btnYES setImage:notSelected forState:UIControlStateNormal];
            [self.btnNO setImage:selected forState:UIControlStateNormal];
        }
    }
}

1 个答案:

答案 0 :(得分:0)

请使用NSUserDefaults在本地存储值。请查看以下内容 -

 - (IBAction)btnCheckBoxfor1:(id)sender {

        UIImage *selected = [UIImage imageNamed:@"checkbox_fill.png"];
        UIImage *notSelected = [UIImage imageNamed:@"checkbox.png"];


        if ([sender tag]==1) { // checkbox yes

            if (![sender isSelected]) {
                [sender setSelected:YES];
                [self.btnNO setSelected:NO];
                [self.btnNO setImage:notSelected forState:UIControlStateNormal];
                [self.btnYES setImage:selected forState:UIControlStateNormal];

                NSUserDefaults *boolUserDefaults = [NSUserDefaults standardUserDefaults];
                [boolUserDefaults setBool:YES forKey:@"CheckBox"];
                [boolUserDefaults synchronize];

            }

        }else{ // checkbox no
            if (![sender isSelected]) {
                [sender setSelected:YES];
                [self.btnYES setSelected:NO];
                [self.btnYES setImage:notSelected forState:UIControlStateNormal];
                [self.btnNO setImage:selected forState:UIControlStateNormal];

                NSUserDefaults *boolUserDefaults = [NSUserDefaults standardUserDefaults];
                [boolUserDefaults setBool:NO forKey:@"CheckBox"];
                [boolUserDefaults synchronize];


            }
        }

获取check的值 -

BOOL isCheck =[[NSUserDefaults standardUserDefaults] boolForKey:@"CheckBox"];

希望这会对你有所帮助。

<强>更新

在ViewWillAppear上调用以下方法 -

- (void)setvalueCheckBox {

    BOOL isCheck =[[NSUserDefaults standardUserDefaults] boolForKey:@"CheckBox"];

    UIImage *selected = [UIImage imageNamed:@"checkbox_fill.png"];
    UIImage *notSelected = [UIImage imageNamed:@"checkbox.png"];


    if (isCheck) { // checkbox yes


            [sender setSelected:YES];
            [self.btnNO setSelected:NO];
            [self.btnNO setImage:notSelected forState:UIControlStateNormal];
            [self.btnYES setImage:selected forState:UIControlStateNormal];

            NSUserDefaults *boolUserDefaults = [NSUserDefaults standardUserDefaults];
            [boolUserDefaults setBool:YES forKey:@"CheckBox"];
            [boolUserDefaults synchronize];



    }
    else{ // checkbox no

            [sender setSelected:YES];
            [self.btnYES setSelected:NO];
            [self.btnYES setImage:notSelected forState:UIControlStateNormal];
            [self.btnNO setImage:selected forState:UIControlStateNormal];

            NSUserDefaults *boolUserDefaults = [NSUserDefaults standardUserDefaults];
            [boolUserDefaults setBool:NO forKey:@"CheckBox"];
            [boolUserDefaults synchronize];



    }
}