UIDatePicker 约束在设备中不起作用

编程入门 行业动态 更新时间:2024-10-25 23:35:47
本文介绍了UIDatePicker 约束在设备中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-29 04:30:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1183998.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   设备   UIDatePicker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!