[英]using the same UIDatePicker for two different UITextFields to input different dates
我有一个包含UIDatePicker的XIB文件。 我将其作为子视图添加到了UI中。 它似乎可以正常运行,但是当我尝试输入日期时却没有这样做。
这是我用来将文本设置为等于在DatePicker中输入的日期的方法:
-(IBAction)datePickerValueChanged:(UIDatePicker *)sender{
myDatePicker = [[myDatePickerView subviews] lastObject];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:sender.locale];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
}
我删除了设置文本的代码,因为它不起作用。 我尝试了一切,但仍然无法正常工作。
PS:因为我不希望键盘出现,所以TextField中没有启动编辑会话。
-(IBAction)datePickerValueChanged:(UIDatePicker *)发送者{
//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.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.