我有复选框Remember Password(登录表单)。我想要当我检查时记住它保存并形成运行它显示复选框检查。如果我取消选中它运行并显示复选框取消选中。请分享我提前谢谢
答案 0 :(得分:6)
将复选框值绑定绑定到共享用户默认控制器上的某个键:
(您没有具体说明这是iOS还是Mac,但由于iOS中没有标准的复选框控件,我假设后者。)
答案 1 :(得分: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];
答案 2 :(得分: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"];
答案 3 :(得分: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"];
}
}