iOS:保存日期选择器的设置
我对内存管理非常陌生,我有一个关于保存日期选择器日期的问题。这是我用来保存输入文本的代码:
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
event1Field.text = [array objectAtIndex:0];
event2Field.text = [array objectAtIndex:1];
}
我如何编辑它来保存日期选择器日期而不是输入文本?我该如何编辑 viewDidLoad 方法?我通常只是在其中输入选择器的数据,如下所示:
- (void)viewDidLoad {
NSDate *now = [NSDate date];
[datePicker setDate:now animated:YES];
}
但我不确定如何将其加载到已保存的状态。抱歉,如果这些是愚蠢的问题,我仍然很新,并且不断学习。
谢谢你!
I'm very new to memory management, and I have a question regarding saving a date pickers date. This is the code that I use to save inputted text:
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
event1Field.text = [array objectAtIndex:0];
event2Field.text = [array objectAtIndex:1];
}
How would I edit this to save a date pickers date rather than inputted text? And how would I edit the viewDidLoad method as well? I usually just input the data of the picker in there, like this:
- (void)viewDidLoad {
NSDate *now = [NSDate date];
[datePicker setDate:now animated:YES];
}
But I'm not sure how I would go about loading it into it's saved state. Sorry if these are dumb questions, I'm still pretty new and learning as I go.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
保存数据的最简单方法是使用 Foundation 框架提供的 NSUserDefaults 。它基本上只是一个键值存储,允许您保存少量数据。
首先也是最重要的,从日期选择器保存数据看起来类似于:
现在,当我们想要将这些数据拉出来并填充我们的日期选择器时,我们可以执行如下操作......
The simplest way to persist data is to use
NSUserDefaults
which is provided by the Foundation framework. It's basically just a key value store which allows you to save small amounts of data.First and foremost, saving data from a date picker looks something akin to this:
Now when we want to pull this data back out and populate our date picker, we could do something like the following...