在选项卡栏中选择时,视图不会重新读取用户默认值
我有一个选项卡栏项目,它在表格视图中显示用户的最爱。我有另一个选项卡栏项目,它显示视图并允许用户添加收藏夹。
更新
这个收藏夹数组是从创建和添加收藏夹的类的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过对其调用同步?
更新下面的注释 - 您的表应该使用将
NSUserDefaults
加载到其中的数组中的数据。您的
cellForRowAtIndexPath
应该看起来像这样(如果您正在重复使用表格单元格,您应该这样做)您可以快速显示数组的内容,以确保在视图出现时它得到更新当视图出现时(假设您已经加载了数组)
Have you tried calling synchronize on it?
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)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)