简体   繁体   中英

NSUserDefaults not saving

I'm trying to save some data and call it back in a tableview from a different controller but it doesn't working. I'm somehow losing a variable value as well, like the category var changes back to zero when I change it in a view controller, any of them.

In my NewEntry.m I have:

-(IBAction)saveButton:(id)sender {    

    int i = selectedSegment.selectedSegmentIndex;    

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    [userDefaults setInteger:i  forKey:@"category"];

    [userDefaults synchronize];

    if (selectedSegment.selectedSegmentIndex == 0) {
        [userDefaults setObject:titlefield.text forKey:@"titletexthomework"];
        [userDefaults setObject:detailstextfield.text forKey:@"detailshomework"];
    }
    else if(selectedSegment.selectedSegmentIndex == 1) {
        [userDefaults setObject:titlefield.text forKey:@"titletextprojects"];
        [userDefaults setObject:detailstextfield.text forKey:@"detailsprojects"];

    }
    else if (selectedSegment.selectedSegmentIndex == 2){
        [userDefaults setObject:titlefield.text forKey:@"titletextappointments"];
        [userDefaults setObject:detailstextfield.text forKey:@"detailsappointments"];
    }
    else if (selectedSegment.selectedSegmentIndex == 3){
        [userDefaults setObject:titlefield.text forKey:@"titletextevents"];
        [userDefaults setObject:detailstextfield.text forKey:@"detailsevents"];

    }

    else if (selectedSegment.selectedSegmentIndex == 4){
        [userDefaults setObject:titlefield.text forKey:@"titletexttodolist"];
        [userDefaults setObject:detailstextfield.text forKey:@"detailstodolist"];
    }

    [userDefaults synchronize];
    NSLog(@"selected segment %i", i);
}

then in my Projects.m I have:

-(void)viewDidLoad
{
    [super viewDidLoad];    

    categoryselected = [[NSUserDefaults standardUserDefaults] integerForKey:@"category"];
    NSLog(@"category selected %i", categoryselected);        

    titlestring = [[NSUserDefaults standardUserDefaults]  objectForKey:@"titletextprojects"];
    detailsstring = [[NSUserDefaults standardUserDefaults] objectForKey:@"detailsprojects"];        

    tabledata = [[NSArray alloc] initWithObjects:titlestring, nil];
    tablesubtitles = [[NSArray alloc] initWithObjects:detailsstring, nil];    
}

//-------------------------------------------------------
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{    
    UITableViewCell *cell = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:@"projectscell"];   

    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"projectscell"];
    }

    cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [tablesubtitles objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:14.0];
    cell.textLabel.backgroundColor = [ UIColor clearColor ];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];

    return cell;
}

** * ** * * UPDATE * ** * ** * ** * ** * *

I Changed the part that populates the table to static string like this:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
tabl = [[NSArray alloc] initWithObjects:@"hello", nil];
NSLog(@"tabledata %@", tabledata );
tab = [[NSArray alloc] initWithObjects:@"hello2", nil];
NSLog(@"details %@", tablesubtitles);



UITableViewCell *cell = nil;

cell = [tableView dequeueReusableCellWithIdentifier:@"projectscell"];


if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"projectscell"];

}
cell.textLabel.text = [tabl objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [tab objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
cell.textLabel.backgroundColor = [ UIColor clearColor ];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];

And the table still shows up blank. Thanks For the help.

设置任何值后调用同步。

I don't notice anything wrong in the NSUserDefaults code, per se, though it's only grabbing the values when the view loads, not when the values are updated. To fix that, you could send a notification to let all other interested view controllers aware of the state change. In -[NewEntry saveButton:], after saving the values in NSUserDefaults, add

[[NSNotificationCenter defaultCenter] postNotificationName:@"ValuesChanged"
                                                    object:self];

to send a notification. Somewhere in Projects.m (init is a good place), subscribe to the notification with

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(valuesChanged:)
                                             name:@"ValuesChanged"
                                           object:nil];

This causes the notification center to call back to your valuesChanged: method when the notification is posted. We'll be updating the UI in pretty much the same way we did in viewDidLoad, so let's factor that code out:

- (void)reloadData
{
    categoryselected = [[NSUserDefaults standardUserDefaults] integerForKey:@"category"];
    NSLog(@"category selected %i", categoryselected);

    // XXX - note the following strings are returned autoreleased. If they're
    // stored in ivars, it's a good idea to retain them, even though we know
    // they're retained by the arrays below.

    titlestring = [[NSUserDefaults standardUserDefaults]  objectForKey:@"titletextprojects"];
    detailsstring = [[NSUserDefaults standardUserDefaults] objectForKey:@"detailsprojects"];

    [tabledata release];
    tabledata = [[NSArray alloc] initWithObjects:titlestring, nil];

    [tablesubtitles release];
    tablesubtitles = [[NSArray alloc] initWithObjects:detailsstring, nil];

    [tableView reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self reloadData];
}

- (void)valuesChanged:(NSNotification*)notification
{
    [self reloadData];
}

One last thing: all those strings, the keys for NSUserDefaults and the notification name @"ValuesChanged"? They really should be defined as constants so that there's only one "true" version of the string. First, this saves you from the case where you accidentally misspell it in one place and wonder why the values aren't syncing up. It can take hours to figure out what's going on when that happens. (Speaking from experience here.) Second, the compiler can then check that you have the right name and the IDE's autocomplete (when it actually works) can suggest key names for you. You can do a #define in a shared header somewhere

#define kDefaultsKeySelectedCategory @"category"

and the linker will probably create a single constant instance of the string shared between every place it's used. Still, if I change the string in that define and Xcode's being cranky and doesn't recompile every source file that uses it, we're back to the case where the key is spelled differently in different places. No good. A fancier way to do this that ensures there's only one copy of the string is to declare

extern NSString* const kDefaultsKeySelectedCategory;

in the header file, then

NSString* const kDefaultsKeySelectedCategory = @"category";

in the .m file. Another thing I like about this way is it hides the implementation details. Nobody needs to know what the specific string is, so it shouldn't be in the header file.

I had the same issue when I was testing my app where if I use Stop in the debugger and then relaunch my app, the NSUserDefaults were incorrectly showing old list. This did not happen with the Insert operation, but with the Delete.

If I use the home button to exit the app (kill it), then relaunch, my NSUserDefaults were correct.

Alec Feb 23 '12 at 17:13 also mentioned this in an earlier comment.

I had an issue with NSUserDefaults not setting, even if you use the synchronize command right after. My issue was because the app was renamed and I believe that the NSUserDefaults didn't synchronize location correctly. I've uninstalled the app and reinstalled it from Xcode and it worked. I guess its a bit of a special case, but if anything try that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM