有時候,特別是如果我玩過設置或者應用程序已經強制退出,所有的格式都會被搞亂,因爲這些值會返回null。任何想法爲什麼這些默認值會返回null?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
bool bold = [[NSUserDefaults standardUserDefaults] boolForKey:@"bold"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
}
int fontSize = [[NSUserDefaults standardUserDefaults] integerForKey:@"fontSize"];
NSString *fontFace = bold? @"Helvetica":[[NSUserDefaults standardUserDefaults] stringForKey:@"fontFace"];
cell.textLabel.font = [UIFont fontWithName:fontFace size:(float)fontSize];
NSString *cellText = romanized? @"Romanized":@"Text";
cell.textLabel.text = [[self.myDataArray objectAtIndex:indexPath.row] objectForKey:cellText];
return cell;
}
編輯從InAppSettingsKit代碼,在我的rootview控制器我這樣做:
- (void)settingsViewControllerDidEnd:(IASKAppSettingsViewController*)sender {
[[NSUserDefaults standardUserDefaults] synchronize];
[self dismissModalViewControllerAnimated:YES];
// your code here to reconfigure the app for changed settings
}
當您在設置NSUserDefaults的價值觀,你然後調用默認'synchronize'對象? –
我正在使用InAppSettingsKit。當它返回到我的rootviewcontroller時,我會進行同步。 – Alienz
我沒有使用過這個庫,所以我無法做出明確的評論。 「同步」是我最好的建議。 :-)也許有些東西在被寫出之前就被破壞了?當然,這個問題可能與NSUserDefaults完全不相關。可能是因爲您的應用程序正在被IDE(如果您在調試器中運行)中死亡或崩潰,無論是哪種情況,對於您的應用程序來說都是一個相當困難的停留,從而無法保留任何數據。 –