-4

我正在尝试在文本字段中的 datePicker 中显示时间。我已经走到这一步了

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(112, 88, 97, 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:textField];

UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
datePicker.timeZone = [NSTimeZone defaultTimeZone];
datePicker.minuteInterval = 5;
datePicker.datePickerMode = UIDatePickerModeTime;

[textField setInputView:datePicker];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle: NSDateFormatterShortStyle];
textField.text = [dateFormatter stringFromDate:datePicker.date];

但是文本字段不会改变

4

1 回答 1

3

你的-(void)datePickerValueChanged:方法是什么样的?

是否如下所示:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle: NSDateFormatterShortStyle];
textField.text = [dateFormatter stringFromDate:datePicker.date];

textField正在编辑的 textField 和datePicker值已更改的日期选择器在哪里?

于 2013-08-05T15:11:17.103 回答