gpt4 book ai didi

ios - UIDatePicker 作为输入 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:14 25 4
gpt4 key购买 nike

我在 UIDatePicker 作为 inputView 有两个问题。

1:当我在 datePicker 中选择“April 17 9 00 PM”时,
dateTextField 输出“2014-04-107 21:00:00”。

2:我所在国家/地区的 UTC 是 +0900。
但是,NSLog 输出“2014-04-17 12:00:00 +0000”。

我该如何解决这些问题?

@property IBOutlet UITextField* dateTextField;
@property IBOutlet UIDatePicker* datePicker;
@property UIToolbar* toolBar;

- (void)viewDidLoad
{
self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 600, 320, 216)];
[self.view addSubview:self.datePicker];
self.toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, -40, 320, 40)];
self.toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dateTextFieldShouldReturn)];
NSArray* finishBarItems = [NSArray arrayWithObjects:doneButton, nil];
[self.toolBar setItems:finishBarItems animated:YES];

self.dateTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 293, 250, 30)];
self.dateTextField.delegate = self;
self.dateTextField.inputView = self.datePicker;
self.dateTextField.inputAccessoryView = self.toolBar;
[myScrollView addSubview:self.dateTextField];
}

- (void)dateTextFieldShouldReturn
{
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-DD HH:mm:ss"];
NSString* dateString = [formatter stringFromDate:self.datePicker.date];
[self.dateTextField setText:dateString];
[self.dateTextField resignFirstResponder];

NSDate* date = [formatter dateFromString:self.dateTextField.text];
NSLog(@"%@", date);
}

最佳答案

像这样对我有用:

- (void)dateTextFieldShouldReturn
{
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];

[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"]];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];


NSString* dateString = [formatter stringFromDate:self.datePicker.date];
[self.dateTextField setText:dateString];
[self.dateTextField resignFirstResponder];

NSDate* date = [formatter dateFromString:self.dateTextField.text];
NSLog(@"%@", date);
}

关于ios - UIDatePicker 作为输入 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23132695/

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