3

我有复选框记住密码(用于登录表单)。我想当我检查时记住它保存并运行它显示复选框检查。如果我取消选中它运行并显示复选框取消选中。请分享给我。预先感谢

4

4 回答 4

7

Bind the checkbox value binding to some key on the shared user defaults controller:

enter image description here

(You didn't specifically say whether this is iOS or Mac, but because there's no standard checkbox control in iOS, I assumed the latter.)

于 2012-09-19T10:09:09.577 回答
1

在 .h 文件上:

@property (nonatomic, unsafe_unretained) IBOutlet NSButton *isCheckedButton;

在 .m 文件上

@synthesize isCheckedButton;

- (IBAction)changeButtonState:(id)sender{
if ([isCheckedButton state]) { // Check if button is checked
    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"YOUR KEY HERE"];
    NSLog(@"Change state to On");
}else{
    [[NSUserDefaults standardUserDefaults] setBool:FALSE forKey:@"YOUR KEY HERE"];
     NSLog(@"Change state to Off");
}
 [[NSUserDefaults standardUserDefaults] synchronize]; // ** DON'T FORGET THIS LINE! ** //
}

上面的代码可能更简单,但这是我认为更好理解的方式。您始终可以通过以下方式读取值:

[[NSUserDefaults standardUserDefaults] boolForKey:@"YOUR KEY HERE"];
于 2013-03-30T23:55:05.387 回答
1

使用-setBool:forKey:方法NSUserDefaults

if(checked)
    [[NSUserDefaults standardUserDefaults] setBool:true forKey:@"RememberMe"];
else
    [[NSUserDefaults standardUserDefaults] setBool:false forKey:@"RememberMe"];

然后你就可以得到一个像这样返回的实际BOOL 值:

BOOL checked = [[NSUserDefaults standardUserDefaults] boolForKey:@"RememberMe"];

编辑:不要忘记添加synchronize此调用NSUserDefaults以立即保存数据

[[NSUserDefaults standardUserDefaults] synchronize];
于 2012-09-19T09:52:43.077 回答
0

你试试这个:

- (void)checkboxButton:(id)sender
 {

 if ( checkboxSelected == 1){

[ checkbox setSelected:NO];
[checkbox setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];

checkboxSelected = 0;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger: checkboxSelected forKey:@"checkboxstate"];

 } else {
[checkbox setSelected:YES];
[checkbox setImage:[UIImage imageNamed:@"checkbox-checked.png"] forState:UIControlStateNormal];
 checkboxSelected = 1;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger: checkboxSelected forKey:@"checkboxstate"];

}



}
于 2012-09-19T11:48:59.400 回答