gpt4 book ai didi

SWIFT 2.2 DatePicker 中的最小和最大 HOUR

转载 作者:搜寻专家 更新时间:2023-10-31 22:45:51 25 4
gpt4 key购买 nike

我正在尝试在我的日期选择器中设置最大和最小小时数。我设法设置了最小值,但无法解析最大值。这是我目前所拥有的:

    let startHour: Int = 8
let endHour: Int = 22
let date1: NSDate = NSDate()
let gregorian: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let components: NSDateComponents = gregorian.components(([.Day, .Month, .Year, .Hour, .Minute]), fromDate: date1)

if components.hour >= 22 && components.hour < 8 {
components.hour = startHour
components.minute = 0
components.second = 0
}

let startDate: NSDate = gregorian.dateFromComponents(components)!

components.hour = endHour
components.minute = 0

let endDate: NSDate = gregorian.dateFromComponents(components)!

picker.datePickerMode = .DateAndTime
picker.minimumDate = startDate
picker.maximumDate = endDate
picker.setDate(startDate, animated: true)

有没有办法在 .maximumDate 方法(现在是我的当前日期)中忽略当前日期和月份?它将我选择的 future 日期设置回当前日期,并仅检查设置为上午 8 点至晚上 10 点的小时间隔。我希望 DatePicker 能够为 future 的每一天启用从早上 8 点到晚上 10 点的选择时间,而不仅仅是当天。

最佳答案

删除 picker.maximumDate = endDate 并编写您自己的自定义验证方法。

// Call datePickerChanged method when the picker finishes rotating
picker.addTarget(self, action: #selector(datePickerChanged(_:)), forControlEvents: .ValueChanged)

func datePickerChanged(picker: UIDatePicker) {
let selectedDate = picker.date
// 1) if selectedDate's hour > than your endHour (10pm)
// 2) create yourNewDate with endHour = 10pm
// 3) set new date with animation
picker.setDate(yourNewDate, animated: true)
}

关于SWIFT 2.2 DatePicker 中的最小和最大 HOUR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38330563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com