我有一个包含UIDatePicker的XIB文件。我将它作为子视图添加到我的UI中。它看起来和功能完美,但当我尝试输入日期时,它不会那样做。
以下是我用来将文本设置为等于DatePicker中输入日期的方法:
-(IBAction)datePickerValueChanged:(UIDatePicker *)sender{
myDatePicker = [[myDatePickerView subviews] lastObject];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:sender.locale];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
}
我删除了设置文本的代码,因为它不起作用。我尝试了一切,但仍然无效。
P.S:TextFields中没有启动编辑会话,因为我不希望键盘出现。
答案 0 :(得分:0)
- (IBAction)datePickerValueChanged:(UIDatePicker *)sender {
//You get the date piker object directly from sender.
//Some how, your date format is in a specific format like this: "DD-MM-YYYY" Convert the input string to Data using NSDateFormatter, like this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:sender.locale];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat:@"DD-MM-YYYY"];
NSDate *date = [dateFormatter dateFromString:@"12-04-2013"];
[sender setDate:date];//here sender is your datePicker, and we are setting the date to that.
}