I have checkbox Remember Password (for login form). and I want when I check remember it save and form run it show checkbox check. if i uncheck it run and show checkbox uncheck. please share me. thank in advance
Asked
Active
Viewed 4,606 times
4 Answers
7
Bind the checkbox value binding to some key on the shared user defaults controller:
(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.)

FluffulousChimp
- 9,157
- 3
- 35
- 42
-
3then this is a perfect use case for Cocoa bindings. – FluffulousChimp Sep 19 '12 at 10:32
-
How do you read that on the code? – Idan Mar 30 '13 at 23:31
-
@Idan: for example like this (in Swift): `NSUserDefaultsController.sharedUserDefaultsController().valueForKeyPath("values.shouldRunCI")` – disco crazy Jun 22 '15 at 20:41
1
Use -setBool:forKey:
method for NSUserDefaults
.
if(checked)
[[NSUserDefaults standardUserDefaults] setBool:true forKey:@"RememberMe"];
else
[[NSUserDefaults standardUserDefaults] setBool:false forKey:@"RememberMe"];
and then you'll be able to get an actual BOOL value returned like this:
BOOL checked = [[NSUserDefaults standardUserDefaults] boolForKey:@"RememberMe"];
EDIT : Don't forgot to add synchronize
as this call NSUserDefaults
to save data immediately
[[NSUserDefaults standardUserDefaults] synchronize];

Paresh Navadiya
- 38,095
- 11
- 81
- 132
1
On .h file:
@property (nonatomic, unsafe_unretained) IBOutlet NSButton *isCheckedButton;
On .m file
@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! ** //
}
Above code could be simpler but this is how I think it's better understood. You can always read the value with:
[[NSUserDefaults standardUserDefaults] boolForKey:@"YOUR KEY HERE"];

Idan
- 9,880
- 10
- 47
- 76
0
you try this:
- (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"];
}
}

Jerry Thomsan
- 1,409
- 11
- 9
-
yeah, I develop on Mac application but your code guide me the idea. thank you. – user1628083 Sep 19 '12 at 13:39