如何获取 datePicker Swift 的初始值

发布于 2025-01-20 05:43:27 字数 475 浏览 2 评论 0原文

我想获得时刻的初始价值,当我只是在滚动时间时,价值会改变。请观看照片,更清楚地了解我想要什么。

https://i.sstatic.net/hj5ga.jpg

@IBAction func datePickerChanged(_ sender: Any) {
    let dateFormatter = DateFormatter()
    dateFormatter.timeStyle = .short
    dateFormatter.dateFormat = "HH:mm"
    let strDate = dateFormatter.string(from: datePicker.date)
    datePickerLb.text = strDate
}

I want to get initial value of timePicker, value changes when I am just scrolling time. Please watch photos it will be more clear to understand what I want.

https://i.sstatic.net/hj5GA.jpg

@IBAction func datePickerChanged(_ sender: Any) {
    let dateFormatter = DateFormatter()
    dateFormatter.timeStyle = .short
    dateFormatter.dateFormat = "HH:mm"
    let strDate = dateFormatter.string(from: datePicker.date)
    datePickerLb.text = strDate
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

池木 2025-01-27 05:43:27

您所需要做的就是更新 viewDidLoad 方法中的标签。我会将日期格式化程序声明移出该方法,以避免每次值更改时创建一个新的声明。请注意,您应该使用 timeStyledateFormat,但不能同时使用两者。向最终用户显示日期时,您应始终遵守设备区域设置和设置,因此在这种情况下应选择 timeStyle


let dateFormatter: DateFormatter = {
    let dateFormatter = DateFormatter()
    dateFormatter.timeStyle = .short
    return dateFormatter
}()

override func viewDidLoad() {
    super.viewDidLoad()
    // your code

    // you can update the label here
    // datePickerLb.text = dateFormatter.string(from: datePicker.date)
    // or manually send the valueChanged action to force the update at view did load
    datePicker.sendActions(for: .valueChanged)
}

@IBAction func datePickerChanged(_ datePicker: UIDatePicker) {
    datePickerLb.text = dateFormatter.string(from: datePicker.date)
}

All you need is to update the label inside your viewDidLoad method. I would move the date formatter declaration out of that method to avoid creating a new one every time the value changes. Note that you should use timeStyle or dateFormat but not both. When displaying dates to the end user you should always respect the devices locale and settings so you should choose timeStyle in this case:


let dateFormatter: DateFormatter = {
    let dateFormatter = DateFormatter()
    dateFormatter.timeStyle = .short
    return dateFormatter
}()

override func viewDidLoad() {
    super.viewDidLoad()
    // your code

    // you can update the label here
    // datePickerLb.text = dateFormatter.string(from: datePicker.date)
    // or manually send the valueChanged action to force the update at view did load
    datePicker.sendActions(for: .valueChanged)
}

@IBAction func datePickerChanged(_ datePicker: UIDatePicker) {
    datePickerLb.text = dateFormatter.string(from: datePicker.date)
}
哽咽笑 2025-01-27 05:43:27

@Leo的回答很实用。需要补充的一件事是,如果您希望在选择器滚动时更新日期,那么不幸的是,UIDatePicker 本身是不可能的。这是因为 UIDatePicker 不是 UIPickerView 的子类,它在内部管理 UIPickerView。这里的解决方案可能是使用自定义的 UIPickerView。然后对于 UIPickerView,您可以像这样实现它的委托方法:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) ->细绳? {

switch component {
case 0:
    currentTime.hours = pickerView.selectedRow(inComponent: 0)
    return "\(row) Hour"
case 1:
    currentTime.minutes = pickerView.selectedRow(inComponent: 1)
    return "\(row) Minute"
case 2:
    currentTime.seconds = pickerView.selectedRow(inComponent: 2)
    return "\(row) Second"
default:
    return ""

}

}

currentTime var 应该有一个 didSet 来在更改时更新某些视图元素。

The answer by @Leo is practical. One thing to add is that if you wanted the date updated while picker it is rolling then, unfortunately, it is impossible with UIDatePicker itself. This is because UIDatePicker is not a subclass of UIPickerView and it manages a UIPickerView internally. The the solution here might be use a custom UIPickerView. Then for that UIPickerView you can implement it's delegate method like this:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

switch component {
case 0:
    currentTime.hours = pickerView.selectedRow(inComponent: 0)
    return "\(row) Hour"
case 1:
    currentTime.minutes = pickerView.selectedRow(inComponent: 1)
    return "\(row) Minute"
case 2:
    currentTime.seconds = pickerView.selectedRow(inComponent: 2)
    return "\(row) Second"
default:
    return ""

}

}

currentTime var should, for example, have a didSet to update some view elements when it changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文