我在 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 actionselector(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);
}
Best Answer-推荐答案 strong>
像这样对我有用:
- (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 作为 inputView,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/23132695/
|