iOS:保存日期选择器的设置

发布于 2024-12-27 20:28:49 字数 627 浏览 3 评论 0原文

我对内存管理非常陌生,我有一个关于保存日期选择器日期的问题。这是我用来保存输入文本的代码:

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 技术交流群。

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

发布评论

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

评论(1

毁梦 2025-01-03 20:28:49

保存数据的最简单方法是使用 Foundation 框架提供的 NSUserDefaults 。它基本上只是一个键值存储,允许您保存少量数据。

首先也是最重要的,从日期选择器保存数据看起来类似于:

// NSUserDefaults is a singleton instance and access to the store is provided 
// by the class method, +standardUserDefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

// Let's pull the date out of our picker
NSDate *selectedDate = [self.datePicker date];

// Store the date object into the user defaults. The key argument expects a 
// string and should be unique. I usually prepend any key with the name 
// of the class it's being used in.
// Savvy programmers would pull this string out into a constant so that 
// it could be accessed from other classes if necessary.
[defaults setObject:selectedDate forKey:@"DatePickerViewController.selectedDate"];

现在,当我们想要将这些数据拉出来并填充我们的日期选择器时,我们可以执行如下操作......

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Get the date. We're going to use a little shorthand instead of creating 
    // a variable for the instance of `NSUserDefaults`.
    NSDate *storedDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"DatePickerViewController.selectedDate"];

    // Set the date on the date picker. We're passing `NO` to `animated:` 
    // because we're performing this before the view is on screen, but after
    // it has been loaded.
    [self.datePicker setDate:storedDate animated:NO];
}

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:

// NSUserDefaults is a singleton instance and access to the store is provided 
// by the class method, +standardUserDefaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

// Let's pull the date out of our picker
NSDate *selectedDate = [self.datePicker date];

// Store the date object into the user defaults. The key argument expects a 
// string and should be unique. I usually prepend any key with the name 
// of the class it's being used in.
// Savvy programmers would pull this string out into a constant so that 
// it could be accessed from other classes if necessary.
[defaults setObject:selectedDate forKey:@"DatePickerViewController.selectedDate"];

Now when we want to pull this data back out and populate our date picker, we could do something like the following...

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Get the date. We're going to use a little shorthand instead of creating 
    // a variable for the instance of `NSUserDefaults`.
    NSDate *storedDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"DatePickerViewController.selectedDate"];

    // Set the date on the date picker. We're passing `NO` to `animated:` 
    // because we're performing this before the view is on screen, but after
    // it has been loaded.
    [self.datePicker setDate:storedDate animated:NO];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文