views:

33

answers:

1

I have a tab bar item which shows users favorites in a tableview. I have another tab bar item which shows a view and allows the user to add a favorite.

Update

this array of favourites gets read from NSUserDefaults in the viewDidLoadMethod of the class which creates and add a favorite.

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSArray *prefs = [def arrayForKey:@"addedQuotes"];  
userAddedQuotes = [[NSMutableArray alloc] initWithArray:prefs];

then this code executes when the user actually adds a favorite

if([text1.text length] && [text2.text length])
{
    NSString *temp = [NSString stringWithFormat:@"%@^%@", 
    text1.text, text2.text];

    [userAdded addObject:temp];

    NSUserDefaults *myDefault = [NSUserDefaults standardUserDefaults];

    [myDefault setObject:userAdded forKey:@"addedFavorites"];

In the tableview class which loads this array from NSUserDefaults to display the values I use the following in the viewWillAppear method.

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSArray *prefs = [def arrayForKey:@"addedFavorites"];   
favorites = [[NSMutableArray alloc] initWithArray:prefs];

At the moment if I add a favorite and then go to the show favorites tab view, it doesnt load the newly added items. only when I exit(press the home button) and start the application again.

How can I get the view to read from the userdefaults again when the tab bar item is selected and the view is shown?

Regards

+2  A: 

Have you tried calling synchronize on it?

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults synchronize];

Update for comments below - Your table should be using the data from the array that gets the NSUserDefaults loaded into it.

Your cellForRowAtIndexPath should look something like this (if you are re-using table cells, which you should be)

...

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];     

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

}

// update cell
cell.textLabel.text = [myUserDefaultsArray objectAtIndex:indexPath.row];
NSLog(@"The table data should be %@", [myUserDefaultsArray objectAtIndex:indexPath.row]);

return cell;

You can quickly display the contents of the array to make sure it's getting updated when the view appears by doing this when the view appears (assuming you've already loaded your array)

int i = 0;

for (i; i<=[myUserDefaultsArray count] - 1; i++) {

    NSLog(@"data is %@ for row %d", [myUserDefaultsArray objectAtIndex:i], i);
}
iWasRobbed
Thanks. I just tried it but no luck.
alJaree
I guess it is because the cellForRowAtIndexPath method is called only the first time the table is setup. I set the cell.text to the objectAtIndex:row of the loaded array from NSUserDefaults. this is not being done after the defaults are updated. How can the tableview be reloaded. [self.tableview reload] freezes the app
alJaree
`cellForRowAtIndexPath` is called everytime a new cell is scrolled into view as well as initially when the table is being created. If the data is in the array, it should populate correctly when the view is shown (assuming you are correctly setting the text of the label on the cell).
iWasRobbed
iWasRobbed
See updated post
iWasRobbed
the table updates on restart. I add to the NSUserdefaults in another class. then read it in the viewWillAppear method of the tableviewcontroller class.
alJaree
I'm not understanding what you mean by `restarts`? Like you quit the app and restart it? Please post some code how you are saving the objects for the NSUserDefaults if that's the case.
iWasRobbed
I updated my question thanks.
alJaree
You need to call `[myDefault synchronize];` after adding your object `[myDefault setObject:userAdded forKey:@"addedFavorites"];` More info here http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html#//apple_ref/occ/instm/NSUserDefaults/synchronize ...Apple states that the user defaults get updated periodically, but if you can't wait for that update (as in your case), you need to call `synchronize` to ensure everything is sync'd and ready to go when the new view appears. Returns `YES` if successfully completed
iWasRobbed
Thanks. I actually tried that with the combination of [tableView reloadData] in the viewWillAppear method and it works. I was trying 1 at a time before and it didnt work. Thanks a lot for the help.
alJaree