NSUserDefaults value not being set
I am trying to set a BOOL to a key in NSUserDefaults, but it doesn't seem to be working as I am getting the NSLog(@"calle开发者_如何学God");
showing up in console every launch. I would really appreciate it if you could tell me where I am going wrong.
for (int i = 0; i < 7; i++) {
CheckBox*btn = [[CheckBox alloc] init];
[btn setFrame:CGRectMake(0, 0, 40, 40)];
[btn setTag:i];
UIImage *img = [UIImage imageNamed:[[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"%i", btn.tag]] ? @"checkbox.png":@"checkbox-pressed.png"];
[btn setImage:img forState:UIControlStateNormal];
[_checkboxArray addObject:btn];
[btn release];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewScrollPositionBottom];
}
if ([[NSUserDefaults standardUserDefaults] boolForKey:kFL]) {
return;
}
else {
NSMutableArray *custArr = [[NSMutableArray alloc] init];
for (int i = 0; i < [_checkboxArray count]; i ++) {
CheckBox *c = (CheckBox *)[_checkboxArray objectAtIndex:i];
[c setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:[NSString stringWithFormat:@"%i", [c tag]]];
[custArr addObject:c];
}
[_checkboxArray removeAllObjects];
[_checkboxArray addObjectsFromArray:custArr];
[custArr release];
[self.tableView reloadData];
NSLog(@"called");
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kFL];
}
Make sure you call
[[NSUserDefaults standardUserDefaults] synchronize];
After you make any changes
精彩评论