gpt4 book ai didi

ios - UIDatePicker返回当前日期,即使我选择其他日期也是如此

转载 作者:行者123 更新时间:2023-12-01 18:54:50 25 4
gpt4 key购买 nike

因此,我有一种方法可以将键盘更改为UIDatePicker,并希望用户选择日期以设置提醒。到目前为止,我已经将键盘替换为UIDatePicker,并且当我打印出日期字符串时,无论我设置了什么,它都只会打印当前日期。

self.datePicker = [[UIDatePicker alloc] init];

[self.noteTextView becomeFirstResponder];
[self.noteTextView setInputView:self.datePicker];

NSDate *date = self.datePicker.date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeZone:NSDateFormatterNoStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];

self.myDate = [dateFormatter stringFromDate:date]; //a string of the date that I want to use in other methods

NSLog(@"Date: %@", [dateFormatter stringFromDate:date]);

最佳答案

UIDatePicker的默认日期是当前日期,这就是您要打印的日期。挑选日期使用

[self.myDatePicker addTarget:self action:@selector(datePickerChanged:) forControlEvents:UIControlEventValueChanged];

- (void)datePickerChanged:(UIDatePicker *)datePicker
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm"];
NSString *strDate = [dateFormatter stringFromDate:datePicker.date];
NSLog(@"Date: %@", strDate);
}

关于ios - UIDatePicker返回当前日期,即使我选择其他日期也是如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28229596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com