在选项卡栏中选择时,视图不会重新读取用户默认值

发布于 2024-09-10 13:35:53 字数 1136 浏览 1 评论 0原文

我有一个选项卡栏项目,它在表格视图中显示用户的最爱。我有另一个选项卡栏项目,它显示视图并允许用户添加收藏夹。

更新

这个收藏夹数组是从创建和添加收藏夹的类的 viewDidLoadMethod 中的 NSUserDefaults 读取的。

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

然后当用户实际

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"];

在 tableview 类中添加收藏夹时执行此代码,该类从 NSUserDefaults 加载此数组以显示我在 viewWillAppear 方法中使用以下值的值。

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

目前,如果我添加收藏夹,然后转到显示收藏夹选项卡视图,它不会加载新添加的项目。仅当我退出(按主页按钮)并再次启动应用程序时。

当选择选项卡栏项目并显示视图时,如何让视图再次从用户默认值中读取?

问候

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

归途 2024-09-17 13:35:53

您是否尝试过对其调用同步?

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

更新下面的注释 - 您的表应该使用将 NSUserDefaults 加载到其中的数组中的数据。

您的 cellForRowAtIndexPath 应该看起来像这样(如果您正在重复使用表格单元格,您应该这样做)

...

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;

您可以快速显示数组的内容,以确保在视图出现时它得到更新当视图出现时(假设您已经加载了数组)

int i = 0;

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

    NSLog(@"data is %@ for row %d", [myUserDefaultsArray objectAtIndex:i], i);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文