NSUserDefaults - 存储和检索数据 [英] NSUserDefaults - storing and retrieving data

查看:134
本文介绍了NSUserDefaults - 存储和检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据是在一个视图中使用NSUserDefaults存储的,然后在另一个视图中显示。我遇到的问题是,当用户更改数据然后返回到显示数据的视图(在UILabel中)时,将显示首次保存的数据而不是新保存的文本。

I have some data that's been stored using NSUserDefaults in one view and then being displayed in another view. The issue I'm having is that when the user changes the data and then returns to the view where the data is displayed (in a UILabel), the data that was first saved is displayed instead of the newer saved text.

我想我可能需要对 viewDidAppear 做一些事情,这样每次显示视图时都会显示最新保存的数据。

I think I need to do something with viewDidAppear perhaps, so that every time the view appears the newest saved data is displayed.

这是我在UILabel上显示NSUserDefaults存储信息的代码:

here's the code that Im displaying the NSUserDefaults stored info on a UILabel:

   NSString *aValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"myTextFieldKey"];
    NSLog(@"Value from standardUserDefaults: %@", aValue);

    NSLog(@"Label: %@", myLabel);
    myLabel.text = aValue;

如果有人能指出我的方向很好,

if someone could point me in the right direction that would be great,

谢谢

推荐答案

将此文本放入

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    NSString *aValue = [[NSUserDefaults standardUserDefaults] objectForKey:@"myTextFieldKey"];
    NSLog(@"Value from standardUserDefaults: %@", aValue);

    NSLog(@"Label: %@", myLabel);
    myLabel.text = aValue;
}

在edit视图中 - viewWillDisappear:保存NSUserDefaults中的更改

And in your "edit" view in - viewWillDisappear: save changes in NSUserDefaults

这篇关于NSUserDefaults - 存储和检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆