我正在尝试将一个BOOL设置为NSUserDefaults中的一个键,但它似乎没有工作,因为我每次启动都会在控制台中显示NSLog(@"called");
。如果你能告诉我哪里出错了,我真的很感激。
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];
}
答案 0 :(得分:2)
请务必致电
[[NSUserDefaults standardUserDefaults] synchronize];
进行任何更改后