问题描述
限时送ChatGPT账号..我在我的项目中使用 UIDatePicker 控件.在xib文件中添加了UIDatePicker,并通过编程方式为datePicker设置了minimumDate、maximumDate等约束条件.这在模拟器中运行良好,但在设备中我可以在 datePicker 中选择任何日期.
I am using UIDatePicker control in my project. Added the UIDatePicker in xib file, and programmatically setting the constraints such as minimumDate, maximumDate to the datePicker. This is working fine in simulator, but in device I am able to select any date in datePicker.
示例代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.datePicker setMinimumDate:[NSDate date]];
[self.datePicker setMaximumDate:[[NSDate date] dateByAddingTimeInterval:3*24*60*60]];
}
截图:
注意:我正在研究 xcode5 - DP6.
Note: I am working on xcode5 - DP6.
提前致谢.
推荐答案
为 UIDatePicker
上的 UIControlEventValueChanged
事件添加一个操作并实施检查以查看所选日期大于最大日期或小于最小日期并相应地设置日期,如下所示:
Add an action for the UIControlEventValueChanged
event on the UIDatePicker
and implement checks to see if the selected date is greater than maximum date or less then the minimum date and set the date accordingly, like so:
- (IBAction)dateValueChanged:(id)sender
{
NSTimeInterval date = [self.datePicker.date timeIntervalSince1970];
NSTimeInterval maxmimumDate = [self.datePicker.maximumDate timeIntervalSince1970];
NSTimeInterval minimumDate = [self.datePicker.minimumDate timeIntervalSince1970];
if (date < minimumDate) {
self.datePicker.date = self.datePicker.minimumDate;
}
if (date > maxmimumDate) {
self.datePicker.date = self.datePicker.maximumDate;
}
// Use the selected date
NSLog(@"%@", self.datePicker.date);
}
如果您选择超出范围的日期,这将导致 UIDatePicker
滚动到最大/最小日期.
This will cause the UIDatePicker
to scroll to the max/min date if you select a date outside the range.
这篇关于UIDatePicker 约束在设备中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论